Lecture 10 5

Subroutine Subprograms

Subroutine subprograms have many features in common with function subprograms:

·  They are program units designed to perform particular tasks under the control of some other program unit.

·  They have the same basic form: each consists of a heading, a specification part, an execution part, and an END statement.

·  They may be internal, module or external subprograms.

·  The scope rules (local vs. global variables) applies to both functions and subroutines.

They differ, however, in the following respects:

·  A function is referenced by using its name in an expression, whereas a subroutine is referenced by a CALL statement

·  Functions return values via function names; subroutine return values via arguments.

·  Functions are designed to return a single value to the program unit that references them. Subroutines often return more than one value, or they may return no value at all but simply perform some task such as displaying a list of instructions to the user.

Consider an example used previously using a function subprogram:

PROGRAM Evaluate_function

REAL :: X,Y,F

DO I =0,10

DO J=0,10

X=I*0.1

Y=J*0.1

A=F(X,Y)

rest of program statements

END DO

END DO

END PROGRAM Evaluate_function

REAL FUNCTION F (X,Y)

REAL :: X,Y

F = X**2 – 3*X*Y +(SIN(X/(1.+Y**3)))**2

END FUNCTION F

We could write the above using a subroutine as follows:

PROGRAM Evaluate_function

REAL :: X,Y,F

DO I =0,10

DO J=0,10

X=I*0.1

Y=J*0.1

CALL Func (F,X,Y)

A=F

rest of program statements

END DO

END DO

END PROGRAM Evaluate_function

SUBROUTINE Func (F,X,Y)

REAL :: F,X,Y

F = X**2 – 3*X*Y +(SIN(X/(1.+Y**3)))**2

END SUBROUTINE Func


Suppose we have available a subroutine which solves a system of "n" linear algebraic equations of the form: where the coefficient matrix [A] and right-hand-side {C} are known values and the unknowns are {x}. The subroutine expects the following information:

N = number of equations

A = NxN coefficient matrix

C = Nx1 right-hand-side matrix

When called, the subroutine will return the array {X} with the solution. The subroutine is called GAUSS and its argument list is (N, A, C, X). To use the subroutine we would simply "call" the subroutine from whatever program we are currently in. For example, lets read the value N, the arrays A and C, call this subroutine to obtain the solution X, then print the results. Our program might look like the following:


PROGRAM SOLVE

REAL::A(10,10), C(10), X(10)

READ *, N

DO I=1,N

DO J=1,N

READ *, A(I,J)

END DO

READ *, A(I)

END DO

CALL GAUSS (N, A, C, X)

DO I=1,N

PRINT *,X(I)

END DO

END PROGRAM SOLVE

SUBROUTINE GAUSS (N, A, C, X)

REAL::A(10,10), C(10), X(10)

statements that solve the simultaneous equation set for {X}

END SUBROUTINE GAUSS

Why use subroutines?

·  They separate a large program into units designed to perform particular tasks under the control of some other program unit.

·  They make working with large programs much easier.

·  They separate large programs into logical units that can be developed and tested individually as subroutine by different persons and then combined with other subroutines for the overall program. The general outline of a large program that contains a main program and several function and subroutine subprograms might be:

PROGRAM Main

main program statements

END PROGRAM Main

SUBROUTINE sub1

Subroutine statements

END SUBROUTINE sub1

more subprograms as needed

FUNCTION func1

Function statements

END FUNCTION func1

More function or subroutine subprograms as needed

·  Once a subroutine for a particular task has been programmed, debugged and verified; it can be incorporated and used in other programs without the need of reprogramming. Solving a set of n equations is a good example of a subroutine that we use often.