Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Recursion / Recursion with multi subs

Quiz — Recursion with multi subs

Given multi fact(0) { 1 } and multi fact($n) { $n * fact($n - 1) }, what role does the multi fact(0) candidate play?

1It is the base case — the candidate dispatch chooses when the argument is 0
0It sets a default value returned for every argument
0It runs first on every call, before the other candidate
0It is required syntax that Raku ignores at run time

The literal 0 in the signature makes that candidate match only when the argument is exactly 0. It is the base case expressed as its own subroutine: while the argument is larger, dispatch keeps choosing multi fact($n), and once the recursion reaches 0, it switches to multi fact(0) and stops.

Course navigation

Recursion with multi subs   |   Power