Course of Raku / Advanced / Modules / Modules basics / Exercises / A maths module

Solution: A maths module

Here is a possible solution to the task. It uses two files.

Code

The module, Maths.rakumod:

unit module Maths;

sub square($n) is export {
    $n * $n
}

The program, maths.raku:

use Maths;

say square(6);

🦋 You can find both source files in the exercises/advanced/modules-basics/maths-module directory.

Output

$ raku -I. maths.raku
36

Comments

  1. The module marks square with is export, which is what makes it available to the program that uses it.

  2. The -I. option puts the current directory on the module search path, so Raku finds Maths.rakumod next to the program.

Course navigation

A maths module   |   A shout module