11.2 SUBROUTINES

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

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."
        READ *,x1, y1
        PRINT *,"Now enter the coordinates of the second point."
        READ *,x2, y2

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.

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

  1. Read the data file.
  2. Echo the data to the monitor.
  3. Compute the mean temperature.
  4. Compute the mean snow depth.
  5. Compute the slope of the regression line.
  6. Output the results to the monitor.
  7. Write the results to a file.

5. Create a graph of your results.