A subprogram is a (small) piece of code that solves a well defined subproblem. In a large program, one often has to solve the same subproblems 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*.

A simple example illustrates how to use a function:

x = cos(pi/3.0)Here

absIn general, a function always has aabsolute valueminminimum valuemaxmaximum valuesqrtsquare rootsinsinecoscosinetantangentatanarctangentexpexponential (natural)loglogarithm (natural)

Now we turn to the user-written functions. Consider the following
problem: A meteorologist has studied the precipitation levels
in the Bay Area and has come up with a model *r(m,t)*
where *r* is the amount of rain, *m* is the month,
and *t* is a scalar parameter that depends on the location.
Given the formula for *r*
and the value of *t*, compute the annual rainfall.

The obvious way to solve the problem is to write a loop
that runs over all the months and sums up the values of *r*.
Since computing the value of *r* is an independent subproblem,
it is convenient to implement it as a function. The following
main program can be used:

program rain real r, t, sum integer m read (*,*) t sum = 0.0 do 10 m = 1, 12 sum = sum + r(m, t) 10 continue write (*,*) 'Annual rainfall is ', sum, 'inches' stop endIn addition, the function

r(m,t) = t/10 * (m**2 + 14*m + 46)The corresponding Fortran function isif this is positiver(m,t) = 0otherwise

real function r(m,t) integer m real t r = 0.1*t * (m**2 + 14*m + 46) if (r .LT. 0) r = 0.0 return endWe see that the structure of a function closely resembles that of the main program. The main differences are:

- Functions have a type. This type must also be declared in the calling program.
- The return value should be stored in a variable with the same name as the function.
- Functions are terminated by the
*return*statement instead of*stop*.

To sum up, the general syntax of a Fortran 77 function is:

typefunctionname (list-of-variables)declarationsstatementsreturn end

The function has to be declared with the correct type in the calling program unit. The function is then called by simply using the function name and listing the parameters in parenthesis.

subroutineNote that subroutines have no type and consequently should not (cannot) be declared in the calling program unit.name (list-of-arguments)declarationsstatementsreturn end

We give an example of a very simple subroutine. The purpose of the subroutine is to swap two integers.

subroutine iswap (a, b) integer a, b c Local variables integer tmp tmp = a a = b b = tmp return endNote that there are two blocks of variable declarations here. First, we declare the input/output parameters, i.e. the variables that are common to both the caller and the callee. Afterwards, we declare the

program callex integer m, n c m = 1 n = 2 call iswap(m, n) write(*,*) m, n stop endThe output from this program is "2 1", just as one would expect. However, if Fortran 77 had been using call-by-value then the output would have been "1 2", i.e. the variables m and n were unchanged! The reason for this is that only the values of ma nd n had been copied to the subroutine iswap, and even if a and b were swapped inside the subroutine the new values would not have been passed back to the main program.

In the above example, call-by-reference was exactly what we wanted.
But you have to be careful about this when writing Fortran code,
because it is easy to introduce undesired *side effects*.
For example, sometimes it is tempting to use an input parameter
in a subprogram as a local variable and change its value. You should
*never* do this since the new value will then propagate back to the
calling program with an unexpected value!

We will come back to this issue in a later section on passing arrays as arguments (parameters).

- Exercise A
- Write a function called
*fac*that takes an integer*n*as input and returns*n!*(n factorial). Test your function using this main program:program tstfac c c Exercise A, section 11. c Main program to test factorial function. c integer n, fac 10 continue write(*,*) 'Give n: ' read (*,*) n if (n.gt.0) then write(*,*) n, ' factorial is', fac(n) goto 10 endif c End of loop stop end

Submit your output for n=5,10,20. Also show how you stopped the program. (Hint: You have to use a loop in your function since Fortran 77 does not allow recursion.) - Exercise B
- Write a subroutine
*qsolve*that takes three real numbers*a,b,c*as input and computes the two real roots of the quadratic*ax**2 + bx + c = 0*. If the roots are complex, you should print an error message likewrite(*,*) 'Warning: Complex roots!'

Also write a main program that tests your subroutine using first the dataa=2.0, b=9.0, c=4.0

and thena=2.0, b=0.0, c=4.0

(As usual, submit your code and the output.)

[AMS Home Page] [Fortran Tutorial Home]

boman@sccm.stanford.edu