`prev Menu next`

Recursive Call ☆
Function definitions and subprograms are allowed to use themselves on their definition.
Example.
The factorial of n (n!) can be defined as follows.
1!=1
n!=n･(n-1)!
The following function FACT calculates factorials using the recursion above.

    10 FUNCTION FACT(n)
    20  IF n=1 THEN
    30    LET FACT=1
    40  ELSE
    50     LET FACT=n*FACT(n-1)
    60  END IF
    70 END FUNCTION
    80 PRINT FACT(10)
    90 END

Supplementary Explanation.
The function FACT is exeuted with n=10 first.
Then to calculate n*FACT(n-1), FACT is executed with n=9 on the line 50.
And then to calculate n*FACT(n-1), FACT is executed with n=8 on the line 50 again.
This is repeated until n becomes 1.
Memory locations for all parameters of an internal function definition are newly gotten for each time they are invoked.
Memory locations for all variables of an external function definition are newly gotten for each time they are invoked.
Memory locations for all parameters except variable parameters of an internal subprogram are newly gotten for each time they are invoked.
Memory locations for all variables except variable parameters of an external subprogram are newly gotten for each time they are invoked.
Memory locations newly gotten are removed when they return.
Note.
In an external procedure, its name need not be declared with a DECLARE EXTERNAL statement within itself.