next up previous contents
Next: Recursive Function Example Up: External Procedures Previous: Side Effect Functions

 

Recursive Procedures

 

Recursion occurs when procedures call themselves (either directly or indirectly). Any procedural call chain with a circular component exhibits recursion. Even though recursion is a neat and succinct technique to express a wide variety of problems. If used incorrectly it may incur certain efficiency overheads.

In FORTRAN 77 recursion had to be simulated by a user defined stack and corresponding manipulation functions, in Fortran 90 it is supported as an explicit feature of the language. For matters of efficiency, recursive procedures (SUBROUTINE s and FUNCTION s) must be explicitly declared using the RECURSIVE keyword. (See below.)

Declarations of recursive functions have a slightly different syntax to regular declarations, the RESULT keyword must be used with recursive functions and specifies a variable name where the result of the function can be stored. (The RESULT keyword is necessary since it is not possible to use the function name to return the result. Array valued recursive functions are allowed and sometimes a recursive function reference would be indistinguishable from an array reference. The function name implicitly has the same attributes as the result name.)

The fact that a function exhibits recursion must be declared in the header, valid declarations are:

    INTEGER RECURSIVE FUNCTION fact(N) RESULT(N_Fact)

or

    RECURSIVE INTEGER FUNCTION fact(N) RESULT(N_Fact)

(In the above the INTEGER applieds to both fact and N_Fact.)

or,

    RECURSIVE FUNCTION fact(N) RESULT(N_Fact)
    INTEGER N_Fact
In the last case INTEGER N_Fact implicitly gives a type to fact; it is actually illegal to also specify a type for fact.

Subroutines are declared using the RECURSIVE SUBROUTINE header.

Return to corresponding overview page gif



next up previous contents
Next: Recursive Function Example Up: External Procedures Previous: Side Effect Functions

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.