September 2017
Beginner
402 pages
9h 52m
English
So far, we have worked with subs that take a few arguments, and their meaning is defined by their position in the argument list; those arguments are called positional. In Perl 6, arguments may also be passed by names. The named parameters can appear in the function call at different positions.
Consider an example of a function that calculates the total amount based on the number of items bought and their price, and prints the total value. With regular positional arguments, the function can look like this:
sub register($item-name, $item-price, $quantity) { my $total = $item-price * $quantity; my $plural-ending = $quantity > 1 ?? 's' !! ''; say "$quantity $item-name$plural-ending cost €$total"; } register('Book', 30, 1); # ...Read now
Unlock full access