|
|||||
Functions: tail recursion | |||||
Prerequisite Concepts | Related Syntax |
OmniMark supports a special type of recursion called tail recursion. If calling itself or another function is the very last thing a function does, then instead of "calling" that function, OmniMark terminates the current function and "jumps" to the other function. The main effect of this is that, with a bit of care, function calls can be made that seem to be millions of levels deep without actually using up any more computer memory than a single call.
The OmniMark programmer doesn't have to do anything to make a function tail recursive. The OmniMark compiler examines each function call in a function and determines if it can be made into a tail recursive call.
The rule that a function call must be the very last thing in a function is interpreted in a very strict way. In particular, a function will not be tail recursive when:
save
is done in the calling function,
using
in the calling function establishes a #current-output
set (using output
) or current group set (using group
), or
do scan
, repeat scan
, or repeat over
has not yet been completed.
In all of these cases something has to be done (or undone) by the calling function after returning from the called function. This cleanup prevents a "jump" to the called function.
Prerequisite Concepts Functions Functions: argument classes Functions: recursive |
Related Syntax define function |
---- |