A recursive program can be designed putting the reference of a blue patch in its own editor – that is, putting a patch within itself . |
If this patch has inputs and outputs, they will also appear on the patch box, so that the patch can be "called from inside itself". |
A termination condition is absolutely necessary in a recursive patch. Otherwise, a succession of infinite calls will be triggered at the evaluation.
Remind to save all your material before calling a recursive patch.
Do not ever build recursive programs with internal – red – patches, or with OMLoop boxes. A function has to be global to apply within itself.
This recursive patch implements a preliminary version of the factorial function. It is called inside itself and calculates fact(n) = n x fact(n-1). This function has no termination condition. If it is called in its current state, it will never end. |
Termination :
The resulting patch means : if n = 1, then fact(n) = 1 else, fact(n) = n x fact(n-1)
|