Course of Raku / Essentials / Functions essentials / More about functions
Typed parameters
Function parameters in Raku can be made type restricted. This is very similar to typed variables.
sub add(Int $x, Int $y) { $x + $y }The function now demands its arguments to be integer numbers.
say add(10, 20);
# say add(pi, e); # ErrorAn attempt to pass a parameter of any other type than
Int is a compile-time error.
$ raku t.raku
===SORRY!=== Error while compiling t.raku
Calling add(Num, Num) will never work with declared signature (Int $x, Int $y)
at t.raku:5
------> say ⏏add(pi, e);Notice that Raku won’t automatically convert types even if it is possible in other cases.
# say add('3', '4'); # Error
say '3' + '4'; # OK and is 7%%tipblock ## How to tell if it’s a compile-time error If the error
message starts with ===SORRY!=== Error while compiling, it
means that the error happened at compile time. %%/tipblock
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Mind the space | Quiz — Typed parameters →
💪 Or jump directly to
the exercise to this section.
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська