Creating and calling functions
You’ve reached the last section of the first part of the course. By completing it, you will have all the knowledge needed to create virtually any program in Raku. The subject of this section is functions, or subroutines, or even simply routines.
A function is a reusable part of code with its own name. You can call functions from different places in the program.
Creating a function
To create a function, use the keyword sub
followed by the name of the function. The body of the function is enclosed in a pair of curly brackets.
sub greet {
say 'Hello, World!';
}
Using a function
To use the function, just put its name at the place where you need to call it. In Raku, a function’s definition can be located before or after the place where the function is used.
say 'This is what the first program usually prints:';
greet;
sub greet {
say 'Hello, World!';
}
The program prints both messages:
$ raku t.raku
This is what the first program usually prints:
Hello, World!
Now, let us look at other details of creating and using functions.
Topics in this section
Exercises
This section contains 6 exercises. Examine all the topics of this section before going to the coding practice.
Course navigation
← Associative data types / Interpolating hashes | Function names →
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська