Course of Raku / Advanced / Subroutines / Anonymous subroutines / Exercises

Triple it

Problem

An anonymous subroutine is a closure: it remembers the variables in scope where it was written. Declare a variable $factor set to 3. Then store a pointy block in $scale that multiplies its argument by $factor (not by a hard-coded 3). Call $scale(7) and print the result.

Example

The program prints:

21

Solution

See the solution

Course navigation

Anonymous subroutines   |   Solution: Triple it