Course of Raku / Essentials / Creating and calling functions
Function parameters
It’s time to pass some data to a function. It can take parameters listed in parentheses immediately after the name of the function.
sub greet($name) {
say "Hello, $name!";
}
You can now use the function and pass different arguments to it:
greet('Alla');
greet('Karl');
The two calls of the same function will now produce different results:
$ raku t.raku
Hello, Alla!
Hello, Karl!
Parameter or argument
These terms are often used interchangeably. They both refer to ‘the same thing’, but look at it from different perspectives.
- A parameter is what the function expects.
- An argument is what you pass to it.
In the above example, $name
is the function’s parameter, while 'Alla'
and 'Karl'
are its arguments.
More parameters
A function can take more than a single parameter. In this case, list all of them comma-separated:
sub add($x, $y) {
say $x + $y;
}
add(10, 20); # 30
No parameters
It is a valid case when the function takes no parameters at all. In this case, you can either add an empty pair of parentheses or omit them completely, as we did at the beginning of this section.
sub greet() {
say 'Hello, World!';
}
greet();
Signature
The list of parameters of a function is called a signature.
Practice
Complete the quizzes that cover the contents of this topic.
Course navigation
← Creating and calling functions / Function names | Creating and calling functions / Returning the result →
💪 Or jump directly to the exercises to this section.
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська