Raku Books / Perl 6 at a Glance / Code Organization / Subroutines, or subs

Typed arguments

Similarly to the above-described typed variables, it is possible to indicate that the sub’s parameters are typed. To do so, add a type name before the name of the parameter.

sub say-hi(Str $name) {
    say "Hi, $name!";
}

If the types of the expected and the actual parameters do not match, a compile-time error will occur.

say-hi("Mr. X"); # OK
# say-hi(123); # Error: Calling say-hi(Int) will never work
               # with declared signature (Str $name)

Course navigation

Non-value argument passing   |   Optional parameters