Next: IF-blocks Up: Basic Fortran Concepts Previous: Formatted Output

## Functions

Fortran provides a useful selection of intrinsic functions to carry out various mathematical operations such as square root, maximum and minimum, sine, cosine, etc., as well as various data type conversions. You can also write your own functions. The next example, which computes the area of a triangle, shows both forms of function in action.

The formulae for the area of a triangle with sides of length a, b, and c is:

s = (a + b + c)/2


PROGRAM TRIANG
WRITE(UNIT=*,FMT=*)'Enter lengths of three sides:'
WRITE(UNIT=*,FMT=*)'Area is ', AREA3(SIDEA,SIDEB,SIDEC)
END

FUNCTION AREA3(A, B, C)
*Computes the area of a triangle from lengths of sides
S = (A + B + C)/2.0
AREA3 = SQRT(S * (S-A) * (S-B) * (S-C))
END

This program consists of two program units. The first is the main program, and it has as similar form to those seen earlier. The only novel feature is that the list of items output by the WRITE statement includes a call to a function called AREA3. This computes the area of the triangle. It is an external function which is specified by means of a separate program unit technically known as a function subprogram.

The external function starts with a FUNCTION statement which names the function and specifies its set of dummy arguments. This function has three dummy arguments called A, B, and C. The values of the actual arguments, SIDEA, SIDEB, and SIDEC, are transferred to the corresponding dummy arguments when the function is called. Variable names used in the external function have no connection with those of the main program: the actual and dummy argument values are connected only by their relative position in each list. Thus SIDEA transfers its value to A, and so on. The name of the function can be used as a variable within the subprogram unit; this variable must be assigned a value before the function returns control, as this is the value returned to the calling program.

Within the function the dummy arguments can also be used as variables. The first assignment statement computes the sum, divides it by two, and assigns it to a local variable, S; the second assignment statement uses the intrinsic function SQRT which computes the square-root of its argument. The result is returned to the calling program by assigning it to the variable which has the same name as the function.

The END statement in a procedure does not cause the program to stop but just returns control to the calling program unit.

There is one other novelty: a comment line describing the action of the function. Any line of text can be inserted as a comment anywhere except after an END statement. Comment lines have an asterisk in the first column.

These two program units could be held on separate source files and even compiled separately. An additional stage, usually called linking, is needed to construct the complete executable program out of these separately compiled object modules. This seems an unnecessary overhead for such simple programs but, as described in the next section, it has advantages when building large programs.

In this very simple example it was not really necessary to separate the calculation from the input/output operations but in more complicated cases this is usually a sensible practice. For one thing it allows the same calculation to be executed anywhere else that it is required. For another, it reduces the complexity of the program by dividing the work up into small independent units which are easier to manage.

Next: IF-blocks Up: Basic Fortran Concepts Previous: Formatted Output
Helen Rowlands
8/27/1998