## 11.2 SUBROUTINES

Subroutines are similar to functions, yet differ from them in several ways

• There is no value associated with the name of the subroutine.
• A CALL statement is used to invoke a subroutine.
• The keyword SUBROUTINE is used in the definition and END statement.
• Subroutines are not required to have any arguments.
The general form is

SUBROUTINE name (argument list)
Specification statements
Executable statements
END

Example - Points Distances

```
PROGRAM points

c This program uses a subroutine to calculate the distance of two
c points from the origin and from each other.
c Variable declarations

REAL x1, y1, x2, y2, d1, d2, d12

c Get coordinates of points.
PRINT *,"Please enter the coordinates of the first point."
PRINT *,"Now enter the coordinates of the second point."

c Calculate the distances
CALL distances (x1, y1, x2, y2, d1, d2, d12)

c Print the results
PRINT *,"The distance of point one from the origin is ",d1
PRINT *,"The distance of point two from the origin is ",d2
PRINT *,"The distance between the points is ",d12

END

SUBROUTINE distances(x1, y1, x2, y2, origp1, origp2, distp1p2)

c Dummy arguments
REAL x1, y1, x2, y2
REAL origp1, origp2, distp1p2

c Calculations
origp1 = SQRT(x1**2 + y1**2)
origp2 = SQRT(x2**2 + y2**2)
distp1p2 = SQRT((x2-x1)**2 + (y2-y1)**2)

END
```

This example illustrates several precepts of functions.

• The statement CALL distances (x1, y1, x2, y2, d1, d2, d12) gives the name of the subroutine and the list of arguments that will be used to transfer values between program units. The arguments in this statement are the actual arguments.

• The statement SUBROUTINE distances (x1, y1, x2, y2, origp1, origp2, distp1p2) is the subroutine heading and contains the subroutine name and the dummy arguments, that is, the arguments used in the subroutine. The actual arguments and the dummy arguments MUST correspond in order and type. The actual arguments in the main program and the dummy arguments in the subroutine match in number, type, and order. They do not need to have the same name. The variables typed in the main program are not known to the subroutine and those typed in the subroutine are not known to the main program.

• The statement REAL x1, y1, x2, y2, origp1, origp2 declares the dummy arguments.

• The statement END is the last statement of the subroutine and transfers control of the program to the line in the calling program unit that is immediately below the CALL statement.

### EXERCISES

1. Rewrite the gestation program from section 7.1.10 with four subroutines: one to read the data, one for EACH of the sorts, and one to print the data.

2. Rewrite the sunspot program from section 7.1.8 with two subroutines: one to read the data and one to do the calculation and write the results to a file.

3. Snowpack Program

Problem Description

Landsat observations led to studies dealing with the evaluation of properties of snowpacks. This is a critical issue since water resources in many areas of the world are heavily dependent on winter snow accumulation.

Computer models have been developed that allow potential water resources to be predicted from satellite measurements of microwave emission in snow-covered areas. Predictions from the models are tested and refined by making comparisons with ground-based measurements of snow depth and temperature. When these measurements are graphed, they show a large amount of scatter. The regression line for the data is used as the standard for comparison. Write a program, using subroutines, that finds the parameters of the equation.

Input/Output Description

The input consists of the data file snow.dat which contains the temperature in degrees Kelvin and the depth of the snow in centimeters. The output should echo the data to the monitor and create a data file consisting of the data and the slope of the regression line, the mean temperature and the mean snow depth.

Algorithm