Program units and procedures

Main program

[program program-name]
end [program [program-name]]

The stop statement

Stops program execution before reaching the end statement, e.g., as part of an if statement.

stop "Message"

External subprograms

External subprograms are called from the main program or elsewhere, usually to perform a well defined task within the framework of a complete program.

end [subroutine [subroutine-name]]

Execution of the end statement returns control to the caller.

Arguments of procedures

Example of a subroutine statement:

subroutine name[(dummy-arguments)]

In the calling program unit:

call name[(actual-arguments)]

Each dummy argument of the called procedure must agree with the corresponding actual argument in type, type parameters, and shape. However, the names do not have to be the same.

Subprograms can be written independently of one another, the association of the dummy arguments with the actual arguments occurring each time the call is executed. In this manner, libraries of subprograms may be built up.

Argument intent

All dummy arguments should be given a declared intent.
intent(in), intent(out), intent(inout)

The return statement

Returns control to the caller before reaching the end statement, e.g., as part of an if statement.



Functions are similar to subroutines in many respects, but they are invoked within an expression and return a value that is used within the expression.

end [function [function-name]]

A type declaration for the function result is required. The type may be defined on the function statement:

[type] function name[(dummy-arguments)]

Example: Celsius to Kelvin conversion

program ck
!convert from Celsius to Kelvin
!Ulrich Schmitt 2007-01-16
real :: tc, tk
print *, "T/C?"
read *, tc
call kelvin(tc, tk)
print *, tk, "K"
subroutine kelvin(tcelsius, tkelvin)
real, intent(in) :: tcelsius
real, intent(out) :: tkelvin
tkelvin = tcelsius + 273.15
end subroutine kelvin
end program ck