递归 - 看递归
递归很容易 - 从程序内部来看程序本身。但是如果你递得太多,那么堆栈将会耗尽空间。
一个程序计算阶乘。
PROCEDURE factorial:
DEFINE INPUT PARAMETER piNum AS INTEGER NO-UNDO.
DEFINE OUTPUT PARAMETER piFac AS INTEGER NO-UNDO.
DEFINE VARIABLE iFac AS INTEGER NO-UNDO.
IF piNum = 1 THEN DO:
pifac = 1.
END.
ELSE DO:
RUN factorial(piNum - 1, OUTPUT iFac).
piFac = piNum * iFac.
END.
END PROCEDURE.
DEFINE VARIABLE f AS INTEGER NO-UNDO.
RUN factorial(7, OUTPUT f).
DISPLAY f.