Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: SUBROUTINE EXAMPLE

It is recommended to look at the numerical methods codes where the use of subroutines is demonstrated. An additional example is included here.
This example calls the subroutine named "linearForm"
!Interpolation loop
DO i=1,SIZE(myout,1),1
	DO j=1,SIZE(datain,1),1
		IF (my(i,1) > da(j,1) .AND. my(i,1) < da(j+1,1)) THEN
			line(1:2,1) = da(j:j+1,1)
			line(1:2,2) = da(j:j+1,2)
			CALL linearForm(line, i)			
 
which is located at the end of the file with the code
CONTAINS
​
!———————————————————————--
SUBROUTINE linearForm(a, row)
 IMPLICIT NONE
 REAL, DIMENSION(2,2) :: a
 INTEGER :: r
​
 !Use linear 1st order Taylor series
 !f(x) = f(x_0) + [(x - x_0)/(x_1 - x_0)] * [f(x_1) - f(x_0)]
 out(r, 2) = a(1,2) + ((out(r,1) - a(1,1)) / &
               (a(2,1) - a(1,1))) * (a(2,2) - a(1,2))
​
END SUBROUTINE linearForm
​
  
Here, CONTAINS is the last line of the main program, and the subroutine follows directly after.