Raku Programming/Subroutines

Subroutines
When it comes to code reuse, the most basic building block is the subroutine. They are not the only building blocks in the toolkit however: Raku also supports methods and submethods, that we'll discuss when we talk about classes and objects.

Subroutines are created with the  keyword, followed by name, an optional list of parameters, and then a block of code.

Blocks
Blocks are groups of code contained in  curly brackets. Blocks serve a number of purposes, including setting code apart, grouping several statements together, and creating a scope for variables.

Defining Subroutines
Subroutines are defined using the  keyword. Here is an example:

The parenthesis are used to define the list of formal parameters to the subroutine. Parameters are like regular  local variables, except they are initialized with values when the subroutine is called. Subroutines can pass a result back to their caller using the  keyword:

Optional Parameters
Optional parameters have a  after them. Also, optional parameters may be given a default value with. Required parameters may have a  after them, although this is the default for positional parameters. All required parameters must be listed before all optional ones.

Named Parameters
Normal parameters are passed by their position: The first passed parameter goes into the first positional argument, the second goes into the second, and so on. However, there is also a way to pass parameters by name, and to do so in any order. Named parameters are basically pairs, where a string name is associated with a data value. Named data values can be passed using either pair or adverb syntax.

Of course, subroutine signatures allow a special shorthand, that you can use if your variable has the same name as the pair has:

In a subroutine declaration, named parameters must come after all required and optional positional parameters. Named parameters are treated as optional by default unless they are followed by a. Actually, you can put a  after required positional parameters as well, but that's the default.

Slurpy Parameters
Raku also allows so called "slurpy" parameters using the *@ syntax.

The *@ tells Raku to flatten out the rest of the arguments into a list and store in the array @theRest. This is necessary to allow perl to accept positional or named arrays without requiring references.

The above code will output three lines:


 * the first argument was: scalar
 * the second argument was: 1, 2, 3
 * the rest were: "foo", "bar"

Calling Subroutines
Once we have a subroutine defined, we can call into it later to retrieve results or actions from it. We've already seen the built-in  function, where you can pass strings to it, and have those strings printed to the console. We can use our  function from above to calculate various values:

We can use the  sigil to store a reference to the subroutine into a normal scalar variable:

Multi Subroutines
In this example, you see that we are passing both integer values and floating point values to our  subroutine. However, we can use our type specifiers to restrict what kinds of values

Raku allows you to write multiple functions with the same name, so long as they have different parameter signatures and are marked with the key word. This is called multi method dispatch, and is an important aspect of Raku programming.

Anonymous Subroutines
Instead of naming a subroutine like normal, we can define an anonymous subroutine and store a reference to it in a variable.

Notice that we could also store these code references in an array: