The FORTRAN Programming Language 

 

Sub-Programs and Storage Management

Subprograms in FORTRAN are indicated with FUNCTION or SUBROUTINE.

Functions
Functions have a type and return a single value of that type. Many functions are provided in the language. These include mathematical functions, trigonometric functions, character operation, and operations for complex numbers. A list of many of these can be found at:
http://www.obliquity.com/computer/fortran/function.html

FORTRAN also allows programmers to define their own functions using FUNCTION. The basic structure of FORTRAN 77 functions are:

                                           <type> FUNCTION <name> (<list of variables>)
                                           <declarations>
                                           <statements>
                                           RETURN
                                           END

<Name> is both the name of the function and the name of the <type> variable who's value will be returned. In FORTRAN 77 the return types of functions is restricted. The list of variables are values that will be used to calculate the value of <name>. Functions themselves can be arguments in the list of parameters, but to limit side-effects, the function cannot change the values of other operands in the expression. FORTRAN 77 does not type check the parameters of subprograms, but FORTRAN 90 does. When passing multi-dimensional arrays as parameters, Pre-90's versions of FORTRAN require the dimensions to also be passed as arguments.

FORTRAN 90 has changed a main program's reference to both functions and subroutines with the INTERFACE block. This block lists the number of arguments, their type and the return type (if any) of the subprograms called. This assists in compile-time type checking. The function declaration has changed as well

                                           FUNCTION <name> (<list of variables>)
                                           <return type> :: <name>
                                           <type 1> [INTENT(IN)] ::<variable 1>
                                           <type 2> [INTENT(IN)] ::<variable 2>
                                           ...
                                           <type n> [INTENT(IN)] ::<variable n>
                                           <declarations>
                                           <statements>
                                           END FUNCTION <name>

The INTENT(IN) qualify indicates that the parameter value is not supposed to be modified by the subprogram.

The declaration of a Subroutine in FORTRAN 90 is similar:

                                           SUBROUTINE <name> (<list of variables>)
                                           <type 1> [INTENT(IN)] ::<variable 1>
                                           <type 2> [INTENT(IN)] ::<variable 2>
                                           ...
                                           <type n> [INTENT(IN)] ::<variable n>
                                           <declarations>
                                           <statements>
                                           END FUNCTION <name>
 

Recursion and subprograms
Pre-90's versions of FORTRAN do not allow recursion of subprograms. FORTRAN 77 allowed the programmer to choose static or stack-dynamic allocation of subprogram variables, but because there could only be one instance of a subprogram record at any time, there was no reason to use dynamic allocation. FORTRAN 90 allows for both nested and recursive subprograms. A subprogram is declared RECURSIVE before the SUBROUTINE or FUNCTION statement.

Parameter Passing
In the case of both functions and subroutines, arguments in the parameter list are called in an in-out mode. This means that if the value of one of the arguments is changed locally in the subprogram, the change will effect the value of the parameter outside the subprogram call. Earlier versions of FORTRAN used call-by-reference where a pointer to the argument is passed rather than only it's value. FORTRAN 90 uses a method called pass-by-value-result. The value of the arguments are passed to the locally declared variable when the function is called. When the function reaches the END or RETURN statement, the current values of the local variable overwrite the value of the argument.

Other Features

FORTRAN 90 provides EXIT to end the execution of a block of text. CONTINUE transfers control to the top of the smallest enclosing loop. Finally, CYCLE transfers control to the bottom of an iterative loop. FORTRAN 90 allows independent compilation and FORTRAN 90 provides separate compilation.