Course of Raku / Essentials / Creating and calling functions

Named parameters

In contract to positional parameters, named parameters are referred by their names.

The following function takes two parameters called $from and $to.

sub distance(:$from, :$to) { $from - $to }

Now, to call the function, you need to name the arguments:

say distance(from => 30, to => 10); # 20

It is an error to pass the arguments as if they were positional. For example, a call distance(30, 10) generates an error:

Too many positionals passed; expected 0 arguments but got 2
    in sub distance at t.raku line 1
    in block <unit> at t.raku line 2

The good part is that named arguments can be listed in any order. The following two calls are totally equivalent:

say distance(from => 30, to => 10); # 20

say distance(to => 10, from => 30); # 20

Passing variables

When the value that you want to pass to a function is kept in a variable, whose name coincides with the name of the parameter, you can enjoy a special syntax that reduces typing:

my $from = 30;
my $to = 10;
say distance(:$from, :$to); # 20

This is similar to a wordy call:

say distance(from => $from, to => $to); # 20

Again, the order is not strict here:

say distance(:$to, :$from); # 20

If the name of the variable differs from the name of the parameter, use one of the ways to pass a pair:

my $a = 20;
my $b = 10;

say distance(from => $a, to => $b);

# or:

say distance(:from($a), :to($b));


Complete the quizzes that cover the contents of this topic.

Course navigation

Creating and calling functions / Positional parameters   |   Creating and calling functions / Default values

💪 Or jump directly to the exercises to this section.