Subroutines are similar to functions, yet differ from them in several ways
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.
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
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.
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.
5. Create a graph of your results.