The pieces a program is built from are the program units (main program, external subprograms, modules, submodules, block data) and the procedures they hold (subroutines and functions). See File structure for how a file holds them.
The main program
program hello
use iso_fortran_env, only: output_unit
implicit none
write (output_unit, '(a)') 'Hello'
contains
subroutine helper()
end subroutine helper
end program hello
The program name line is optional (in FORTRAN 77 a program began with whatever came first), and the name after end program must match if it is given. A program can contain
internal procedures. stop ends the run.
Subroutines and functions
subroutine swap(a, b)
real, intent(inout) :: a, b
real :: t
t = a; a = b; b = t
end subroutine swap
function norm(v) result(r) ! result clause names the result variable
real, intent(in) :: v(:)
real :: r
r = sqrt(sum(v**2))
end function norm
real function area(r) ! type in the header: the old style
real r
area = 3.14159 * r * r ! result is the function's own name
end function area
pure elemental real function sq(x) ! prefixes
real, intent(in) :: x
sq = x * x
end function sq
recursive function fact(n) result(f)
integer, intent(in) :: n
integer :: f
f = merge(1, n * fact(n - 1), n <= 1)
end function fact
| Prefix | Meaning | Since |
|---|---|---|
recursive | may call itself, directly or through other procedures | F90 |
pure | no side effects: it may not change anything outside itself, perform input/output or stop. May be called from do concurrent and forall | F95 |
elemental | written for scalars and usable on arrays; implies pure | F95 |
impure | with elemental, allows side effects | F2008 |
module | a module procedure whose interface is in a module and body in a submodule | F2008 |
non_recursive | the procedure is not recursive | F2018 |
simple | a pure procedure with a further restriction: it may not use variables from outside its own arguments | F2023 |
A function result may be given a type in the header or a declaration. If there is no result clause, the function’s name is the result variable.
Arguments
- Dummy arguments are the names in the procedure’s own header; actual arguments are what the caller passes. Fortran passes by reference unless the dummy has
value(Fortran 2003) or the call uses%val(x)(VAX and GNU extension) intent(in),intent(out)andintent(inout)say the direction, and compilers check it- keyword arguments:
call plot(x, y, colour='red', width=2), in any order after the positional ones, for procedures with an explicit interface optionaldummies can be left out, andpresent(name)asks whether one was given- array dummies: explicit shape
a(n), assumed shapea(:), assumed sizea(*), assumed ranka(..) - procedure dummies:
external fin the old style,procedure(iface) :: fsince Fortran 2003 - alternate return:
subroutine s(a, *, *),call s(x, *10, *20),return 2: obsolescent since Fortran 95
An explicit interface (the compiler can see the procedure’s header) is needed for assumed-shape arguments, keyword arguments, optional, pointer and allocatable dummies,
array-valued and elemental functions and generic names. Module procedures, internal procedures and procedures with an interface block have one; an external procedure has only an implicit interface,
which is all FORTRAN 77 had.
Modules
module geometry
use iso_fortran_env, only: real64
implicit none
private ! everything hidden unless made public
public :: area, pi
real(real64), parameter :: pi = 3.14159265358979_real64
contains
pure function area(r)
real(real64), intent(in) :: r
real(real64) :: area
area = pi * r**2
end function area
end module geometry
program use_geometry
use geometry ! everything public
use geometry, only: area ! or only some names
use geometry, only: sq_area => area ! rename
use, intrinsic :: iso_c_binding ! a standard module
use, non_intrinsic :: geometry
end program
- a module holds declarations (types, constants, variables, interfaces) and, after
contains, module procedures. It was added in Fortran 90 and replacescommonblocks and include files privateandpublicset the default and individual names;protectedlets other units read but not change a variable (Fortran 2003)usemakes names available;only:limits them andlocal => namerenames. The modules a module uses are inherited unless hidden.intrinsicandnon_intrinsicchoose between a standard module and a user one of the same name (Fortran 2003)- a module is compiled before the units that
useit, producing a.modfile
Submodules
module matrices
interface
module function det(a) result(d) ! interface only: no body here
real, intent(in) :: a(:, :)
real :: d
end function det
end interface
end module matrices
submodule (matrices) matrices_impl ! parent module in parentheses
contains
module procedure det ! the body, written without repeating the header
d = 0.0
end procedure det
end submodule matrices_impl
A submodule (Fortran 2008) lets the implementation of a module change without recompiling the units that use the module. submodule (parent:sub) name makes a submodule of a submodule.
A module procedure body in the submodule takes its arguments from the interface.
Interface blocks and generic names
interface ! an explicit interface for an external procedure
subroutine dgemm(ta, tb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
character :: ta, tb
integer :: m, n, k, lda, ldb, ldc
double precision :: alpha, beta, a(lda, *), b(ldb, *), c(ldc, *)
end subroutine dgemm
end interface
interface swap ! one generic name for several procedures
module procedure swap_real, swap_int
end interface swap
abstract interface ! a pure description of a procedure shape (Fortran 2003)
function binary_op(a, b) result(c)
real, intent(in) :: a, b
real :: c
end function binary_op
end interface
procedure(binary_op), pointer :: op => null()
- a generic interface lets one name call different procedures chosen by the types, kinds and ranks of the arguments. The intrinsic procedures work that way (
absof an integer or a real) interface operator(+),interface assignment(=)andinterface read(formatted)orwrite(unformatted)define operators, assignment and user-defined derived-type input/output (Fortran 2003)importinside an interface body brings names from the host into it;import, only:,import, noneandimport, allare Fortran 2018- an
interfacebody may hold abind(c)function: the way to call a C library (below)
Internal procedures and host association
A procedure after contains in a program or another procedure is internal. It sees the host’s variables (host association), and cannot itself contain procedures. An internal procedure can be passed as an
actual argument, and from Fortran 2008 can be the target of a procedure pointer.
BLOCK DATA and ENTRY
BLOCK DATA INIT
COMMON /GRID/ NX, NY
DATA NX, NY /100, 200/
END
SUBROUTINE SOLVE(A, N)
ENTRY SOLVE2(A, N, TOL)
RETURN
END
block data is the program unit that gives initial values to named common blocks. entry adds another name and argument list to a subprogram. Both are old features; modules and separate procedures replace them.
C interoperability
use, intrinsic :: iso_c_binding, only: c_int, c_double, c_char, c_ptr, c_null_char
interface
function c_sqrt(x) bind(c, name='sqrt') result(y)
import :: c_double
real(c_double), value :: x
real(c_double) :: y
end function c_sqrt
end interface
sqrt: bind(c) names the C symbol and value passes the argument the way C does.Fortran 2003 added interoperability with C: bind(c) procedures, types and variables, the kinds of iso_c_binding, and c_loc, c_f_pointer and so on
(see Intrinsic modules). Fortran 2018 added assumed-type and assumed-rank dummies (type(*), dimension(..)) for C.