Course of Raku / Advanced / Subroutines / Signatures / Exercises / Sum all arguments

Solution: Sum all arguments

Here is a possible solution to the task.

Code

sub total(*@nums) {
    [+] @nums
}

say total(3, 5, 7);

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

Output

15

Comments

  1. The slurpy parameter *@nums collects all the arguments into the array @nums, however many there are.

  2. The reduction meta-operator [+] then adds them all together, giving 15.

Course navigation

Sum all arguments   |   An optional exponent