FORTRAN Reference Manual

Program Units
FORTRAN Reference Manual528615-001
4-7
Subroutines
In the next example, the value returned by the function is determined when subroutine
SUBA returns a value for NUMBER:
FUNCTION number(a,b,c)
.
CALL suba(number,x)
.
END
SUBROUTINE suba(j,k)
READ (5,*) k
j = k*3.2
.
END
Subroutines
A subroutine subprogram performs a procedure for a calling program unit. The calling
program unit can be the main program, a function subprogram, another subroutine, or
the subroutine itself.
The subroutine subprogram executes when a CALL statement invokes its name.
Example 4-1 shows the relation between the calling program unit and the subroutine
subprogram:
Example 4-1. Calling Program and Subroutine
PROGRAM numbers
INTEGER x,y
READ(5,4) x, y
FORMAT (2I2)
IF (x .LT. y) CALL error1 <-- subroutine call
result = x * 100
STOP
END
SUBROUTINE error1 <-- subroutine name
WRITE (6,1)
1 FORMAT (5x, 'Number is out of range')
RETURN
END