7.2.10 Recursion

CSI(C&F) procedures may be used recursively; that is, a procedure may call itself either directly or indirectly. In contrast to recursion in most other modern programming languages, recursion in CSI(C&F) has to consider the fact, that all variables are global and no local data is created on a recursive call of a procedure. A brief example should illustrate this fact:

/* recursive procedure calls */

IMPORT VARIABLES My_Vars.dat

LET count = number_of_factors

LET product = 1

CALL prod

PRINT product

EXIT

/* prod is called again and again, */

/* as long as count is greater than 0 */

PROCEDURE prod

IF (count>0) THEN

LET product = product*factor[count]

LET count = count-1

CALL prod

ENDIF

RETURN

This program has the same functionality as the one shown in 7.2.8. The FOR-NEXT loop is replaced by the recursive call of the procedure prod. Recursive code is often more compact and easier to read than the non-recursive equivalent.