Recursion
A procedure that can reference itself, directly or indirectly,
is called a recursive procedure. Such a procedure can reference itself
indefinitely until a specific condition is met. For example, you can
determine the factorial of the positive integer N as
follows:
INTEGER N, RESULT
READ (5,*) N
IF (N.GE.0) THEN
RESULT = FACTORIAL(N)
END IF
CONTAINS
RECURSIVE FUNCTION FACTORIAL (N) RESULT (RES)
INTEGER RES
IF (N.EQ.0) THEN
RES = 1
ELSE
RES = N * FACTORIAL(N-1)
END IF
END FUNCTION FACTORIAL
END
For details on syntax and rules, see FUNCTION, SUBROUTINE, or ENTRY.
You can also call external procedures recursively when you specify the -qrecur compiler option, although XL Fortran disregards this option if the procedure specifies either the RECURSIVE or RESULT keyword.