FORTRAN Subroutines and Functions

Home Computational Physics FORTRAN FORTRAN Subroutines and Functions


Subroutines and Functions
When a program is more than a few hundred lines long, it gets hard to follow. Fortran codes that solve real engineering problems often have tens of thousands of lines. The only way to handle such big codes is to use a modular approach and split the program into many separate smaller units called subprograms.

A subprogram is a (small) piece of code that solves a well-defined sub-problem. In a large program, one often has to solve the same sub-problems with many different data. Instead of replicating code, these tasks should be solved by subprograms. The same subprogram can be invoked many times with different input data.

Fortran has two different types of subprograms, called functions and subroutines.

Functions
Fortran functions are quite similar to mathematical functions: They both take a set of input arguments (parameters) and return a value of some type. In the preceding discussion we talked about user defined subprograms. Fortran also has some built-in functions.

A simple example illustrates how to use a function:

X = cos(45.24)
Here cos is the cosine function. There are many built-in functions in Fortran. Some of the most common are:
abs absolute value
sqrt square root
sin sine
cos cosine
atan arctangent
exp exponential (natural)
log logarithm (natural)

In general, a function always has a type. Most of the built-in functions mentioned above, however, are generic. So in the example above, pi and x could be either of type real or double precision. The compiler would check the types and use the correct version of cos (real or double precision). Unfortunately, Fortran is not really a polymorph language so in general you have to be careful to match the types of your variables and your functions!

Now we turn to the user-written functions. Consider the following problem:

Write a functions to find the maximum value of a one dimensional array ?
C Use of Functions
DIMENSION I(10)
WRITE(6,*) 'Enter 10 Integer numbers, separated by space ?'
READ(5,*) (I(J),J=1,10)
C Display the maximum value
WRITE(6,*) 'Maximum value is = ',MAXIMUM(I,10)
STOP
END
C Function to find the maximum value
FUNCTION MAXIMUM(IARRAY, NSIZE)
DIMENSION IARRAY(NSIZE)
MAXIMUM=IARRAY(1)
DO 10 J=2,10
IF (MAXIMUM.LT.IARRAY(J)) MAXIMUM=IARRAY(J)
10    CONTINUE
RETURN
END


This program fined the factorial value
C Function to find the factorial value
WRITE(6,*) ' Enter an integer to find the factorial ?'
READ(5,*) I
WRITE(6,*) ' Factorial value : ',FACT(I)
RETURN
END
FUNCTION FACT(N)
FACT=1
DO 10 J=2,N
FACT=FACT*J
10    CONTINUE
RETURN
END


Subroutines
A Fortran function can essentially only return one value. Often we want to return two or more values (or sometimes none!). For this purpose we use the subroutine construct.

Example This program finds the minimum and maximum from one-dimensional array.
C Use of Subroutines
DIMENSION I(10)
WRITE(6,*) 'Enter 10 Integer numbers, separated by space ?'
READ(5,*) (I(J),J=1,10)
C Display the maximum value   
CALL MAXMIN(I,10,MAX,MIN)
WRITE(6,*) 'Maximum value is = ',MAX 
WRITE(6,*) 'Minimum value is = ',MIN
STOP
END
C Function to find the maximum value
SUBROUTINE MAXMIN(IARRAY, NSIZE, MAX, MIN)
DIMENSION IARRAY(NSIZE)
MAX=IARRAY(1)
MIN=IARRAY(1)
DO 10 J=2,10
IF (MAX.LT.IARRAY(J)) MAX=IARRAY(J) 
IF (MIN.GT.IARRAY(J)) MIN=IARRAY(J)
10    CONTINUE
RETURN
END
Note : To bring SUBROUTINES into operation we write a CALL statement.


Main differences between FUNCTIONS and SUBROTINES
A SUBROUTINE has no value associated with its name. All outputs are defined in terms of arguments; there may be any number of outputs.
A SUBROUTINE is not called into action simply by writing its name, since no value is associated with the name. Instead, we write a CALL statement to bring it into operation; this specifies the arguments and results in storing all the output values.
Since the output of a SUBROUTINE may b any combination of the various types of values, there is no type associated with the name and likewise no convention attached to the first letter of the name.


The COMMON statement.
It has been stated that each subprogram has its own variable name: the name X in the main program is not necessarily taken to be the same as the name X in a subprogram. However, if the programmer wishes them to mean the same thing, he can write

COMMON X

in both the main program and the subprogram. The compiler will then assign the two variables (and they still are distinct, in principle) to the same storage location, which as a practical matter makes them the same.

But the statement is not limited to this kind of use. Suppose we write
Main program : COMMON X, Y, I
Subprogram : COMMON A, B, J

Then X and A are assigned to the same storage location, as are Y and B and I and J.

Example � This program finds the minimum and maximum from one-dimensional array using COMMON statement.
C Use of COMMON statement
COMMON I(10), MIN, MAX
WRITE(6,*) 'Enter 10 Integer numbers, separated by space ?'
READ(5,*) (I(J),J=1,10)
C Display the maximum value   
CALL MAXMIN
WRITE(6,*) 'Maximum value is = ',MAX 
WRITE(6,*) 'Minimum value is = ',MIN
STOP
END
C Function to find the maximum value
SUBROUTINE MAXMIN
COMMON I(10), MIN, MAX
MAX=I(1)
MIN=I(1)
DO 10 J=2,10
IF (MAX.LT.I(J)) MAX=I(J) 
IF (MIN.GT.I(J)) MIN=I(J)
10    CONTINUE
RETURN
END