LF Fortran 95

Subroutines

A subroutine is a self-contained procedure that is invoked using a CALL statement. For example,

program main implicit none interface ! an explicit interface is provided subroutine multiply(x, y) implicit none real, intent(in out) :: x real, intent(in) :: y end subroutine multiply end interface real :: a, b a = 4.0 b = 12.0 call multiply(a, b) print*, a end program main subroutine multiply(x, y) implicit none real, intent(in out) :: x real, intent(in) :: y multiply = x*y end subroutine multiply

This program calls the subroutine multiply and passes two REAL actual arguments, a and b. The subroutine multiply's corresponding dummy arguments, x and y, refer to the same storage as a and b in main. When the subroutine returns, a has the value 48.0 and b is unchanged.

Subroutine definition syntax:

subroutine-stmt

[use-stmts]

[specification-part]

[execution-part]

[internal-subprogram-part]

end-subroutine-stmt

Where:

subroutine-stmt is a SUBROUTINE statement.

use-stmts is zero or more USE statements.

specification-part is zero or more specification statements.

execution part is zero or more executable statements.

internal-subprogram-part is

CONTAINS procedure-definitions

procedure-definitions is one or more procedure definitions.

end-subroutine-stmt is

END [ SUBROUTINE [subroutine-name] ]

subroutine-name is the name of the subroutine.