Functions and recursion
Reuse calculations, preserve local variables, and trace recursive calls.
On this page
Read functions and recursion first. Each block is a complete program that can be opened independently in the editor.
Reuse a digit-sum calculation
FUNCTION digitSum(n) total = 0 WHILE n > 0 total = total + n MOD 10 n = n DIV 10 END RETURN totalENDx = 4072PRINT digitSum(x)PRINT digitSum(99)PRINT xThe output is 13, 18, 4072. Parameter n changes inside the call without changing x. Each call owns its total. The function assumes a non-negative integer and correctly returns 0 for zero.
Recursive factorial
FUNCTION factorial(n) IF n == 0 THEN RETURN 1 END RETURN n * factorial(n - 1)ENDPRINT factorial(5)The result is 120. Calls descend from 5 to 0; the base case returns 1, then each call multiplies the returned value by its own argument. Test 0 and 1: both have factorial 1.
The argument must be a non-negative integer. Do not run this example with negative or fractional arguments: they do not reach the base case. Execution has step and active-call limits; use small values. Large results also lose numeric precision.
Try it
Add isPalindrome(n) returning a Boolean, using the numeric reversal example. Compare iterative and recursive versions of the sum from 1 through n. Use RETURN for a value needed in other calculations and PRINT for output.