9.2. Named parameters

Example 9-3. examples/subroutines/named_definitions.p6

#!/usr/bin/perl6
use v6;

sub add ($a, $b) {
    return $a + $b;
}

say "first: ", add(2, 3);      # returns 5
#say add(2);        # is an error
#say add(2, 3, 4);  # is an error

sub sum (@numbers) {
    my $sum = 0;
    for @numbers -> $num {
        $sum += $num;
    }
    return $sum;
}

# Now you can already call sum(2) and sum(2, 3) and sum(2, 3, 4) and they
say sum(2); #??
say sum((2, 3));
say sum((2, 3, 4));


sub sumsum (*@numbers) {
    my $sum = 0;
    for @numbers -> $num {
        $sum += $num;
    }
    return $sum;
}

say sumsum(2);
say sumsum(2, 3);
say sumsum(2, 3, 4);
say sumsum((2, 3, 4));

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed