Course of Raku / Advanced / Subroutines / Nested subroutines / Exercises / An inner sum

Solution: An inner sum

Here is a possible solution to the task.

Code

sub compute {
    sub add($x, $y) {
        $x + $y;
    }

    add(2, 3) + add(4, 5);
}

say compute;

🦋 You can find the source code in the file inner-sum.raku.

Output

14

Comments

  1. The nested add is used twice inside compute: add(2, 3) is 5 and add(4, 5) is 9.

  2. Their sum, 14, is the value returned by compute.

Course navigation

An inner sum   |   Quiz — Nested scope