Subroutines & signatures

Named parameters & arguments

Full

Pass arguments by name with :$x parameters and the :$var / key => value forms.

A parameter written :$name is named, not positional: the caller supplies it by name, in any order. Named arguments make call sites self-documenting, especially when there are several.

Declaring and passing named parameters #

sub area(:$w, :$h) {
    $w * $h;
}
say area(w => 3, h => 4);
Output
12

Order is irrelevant — area(h => 4, w => 3) gives the same result.

The :$var shortcut #

When you already have variables of the same name, :$w at the call site is short for w => $w. This pairs neatly with :$w parameters.

sub area(:$w, :$h) {
    $w * $h;
}
my ($w, $h) = 3, 4;
say area(:$w, :$h);
Output
12

Notes #