A program unit starts with its specification part: use statements, then import, then implicit, then
declarations, in that order. Executable statements follow. This page describes what the declarations can say.
Intrinsic types and kinds
| Type | Values | Kinds |
|---|---|---|
integer | whole numbers | selected_int_kind(r) gives the kind for r decimal digits; int8 int16 int32 int64 in iso_fortran_env |
real | floating-point numbers | selected_real_kind(p, r); real32 real64 real128 |
double precision | a real with more than the precision of the default real | one kind; kept from FORTRAN 66 |
complex | a pair of reals; double complex is the DEC and Intel extension for a complex of two double precision parts | the kind of its parts |
logical | .true. and .false. | selected_logical_kind(bits) (Fortran 2023); logical_kinds |
character | strings of a fixed or variable length | selected_char_kind('ascii' | 'iso_10646' | 'default') |
byte | extension: a one-byte integer (integer(1)) | — |
FORTRAN 66 and 77 had the first five types (plus double precision and complex) with no kinds; Fortran 90 added kinds and the kind= selector. The
numbers of kinds are the compiler’s, and most compilers use the number of bytes, so real(8) is the 64-bit real. Portable code asks for what it needs:
use iso_fortran_env, only: int64, real64
integer, parameter :: dp = selected_real_kind(15, 307)
integer(int64) :: count
real(dp) :: x
real(kind=real64) :: y
real*8 :: old_style ! extension, not standard
integer*4 :: also_old
*n forms that are common in old code but are not standard.Declaration forms
integer :: i, j = 5 ! j starts at 5 (and is saved)
real, dimension(3, 3), intent(in) :: a ! attributes, then ::
character(len=20) :: name
character(len=*), intent(in) :: text ! length taken from the argument
character(len=:), allocatable :: line ! deferred length
type(point) :: p
class(shape), allocatable :: s
logical, parameter :: debug = .false.
complex(dp) :: z = (1.0_dp, 2.0_dp)
The old form has no :: and no attributes: integer i, j, real x(10), character*10 name. It is still valid. In a declaration with
:: the viewer colours the attributes before it as keywords and the names after it as plain; without :: the words after the type are names.
Attributes
| Attribute | Meaning | Since |
|---|---|---|
allocatable | space is obtained by allocate; arrays since Fortran 90 (TR 15581 for components and arguments), scalars and automatic reallocation in Fortran 2003 | F90 |
asynchronous | may be changed by asynchronous input/output | F2003 |
bind(c), bind(c, name='x') | C-compatible layout or name | F2003 |
codimension[…] | a coarray | F2008 |
contiguous | the array is contiguous in memory | F2008 |
dimension(…) | the shape of an array | F90 |
external | the name is an external procedure | F77 |
intent(in), intent(out), intent(inout) | direction of a dummy argument | F90 |
intrinsic | the name is an intrinsic procedure | F77 |
optional | the argument may be left out | F90 |
parameter | a named constant | F77 |
pointer | may point at a target | F90 |
private, public | whether other units may use the name | F90 |
protected | a module variable others may read but not change | F2003 |
save | keeps its value between calls | F77 |
target | may be pointed at | F90 |
value | the argument is passed by value | F2003 |
volatile | may change by means outside the program | F2003 |
kind, len | a type parameter of a parameterised derived type | F2003 |
automatic, static | extension: storage class (DEC; GNU Fortran -fdec-static) | — |
device, managed, constant, shared, pinned, texture | CUDA Fortran data attributes | — |
Derived types and their components have further attributes (extends, abstract, sequence, pass, nopass, deferred, non_overridable);
see Derived types and objects. Each attribute can also be given by its own statement (allocatable :: a, dimension a(10), intent(in) :: x);
that is the only way in FORTRAN 77 (dimension, external, intrinsic, parameter, save).
Implicit typing
A name that is not declared takes its type from its first letter: i to n are integer, the other letters real (the “I–N rule” of FORTRAN 66 onwards).
Because misspelled names then become new variables, modern code starts every unit with implicit none.
implicit none ! no implicit typing
implicit none (type, external) ! Fortran 2018: also require explicit interfaces
implicit real (a-h, o-z) ! change the rule for these letters
implicit double precision (a-h, o-z)
implicit undefined (a-z) ! DEC: the same as implicit none
Arrays and shapes
| Shape | Written | Meaning |
|---|---|---|
| explicit shape | a(10), a(0:9), b(n, m) | bounds given by expressions; up to 7 dimensions in FORTRAN 77, 15 in Fortran 2008 |
| assumed shape | a(:), a(0:) | a dummy argument that takes the shape of its argument (needs an explicit interface) |
| deferred shape | a(:) with allocatable or pointer | shape set by allocate or pointer assignment |
| assumed size | a(*), b(n, *) | last dimension unknown; the old way for dummy arrays |
| implied shape | c(*) = [1, 2, 3] with parameter | a named constant array that takes its shape from the value (F2008) |
| assumed rank | x(..) | any rank, for C interoperability (F2018) |
| coarray | a[*], a(10)[2, *] | one copy per image (F2008) |
Arrays are stored in column-major order. An array of rank 0 is a scalar; size, shape, lbound and ubound ask for its extents.
See Expressions and arrays for sections and constructors.
Pointers and allocatables
real, allocatable :: work(:)
real, pointer :: p(:) => null()
real, target :: data(100)
allocate(work(n), stat=ierr)
p => data(1:50) ! p is an alias of part of data
if (associated(p)) print *, size(p)
nullify(p)
deallocate(work)
allocatable variables are freed automatically when they go out of scope and are the first choice; pointer is for aliases and linked structures. Procedure pointers
(procedure(f), pointer :: fp) arrived in Fortran 2003. Cray pointers (pointer (ptr, pointee)) are an extension of Cray and GNU Fortran (-fcray-pointer).
Named constants and initial values
integer, parameter :: n = 5, or the FORTRAN 77 statementparameter (n = 5)- an initial value after
=:integer :: i = 0. In Fortran 90 and later a variable that is initialised has thesaveattribute datastatement:data a, b /1.0, 2.0/,data (x(i), i = 1, 5) /5*0.0/. Repeat counts, implieddoand substrings are allowed. FORTRAN 77 had it as the only way to initialise a variable; putting adatastatement among executable statements is obsolescentblock datais the program unit that initialises a namedcommonblock (FORTRAN 66 onward)- array constructors and
reshapebuild array initial values:integer :: m(2,2) = reshape([1, 2, 3, 4], [2, 2])
COMMON, EQUIVALENCE and SAVE
COMMON /GRID/ NX, NY, DX(100)
COMMON A, B ! blank common
EQUIVALENCE (X, IX), (A(1), B(1))
SAVE COUNT
common puts variables in shared, named storage, so that separate procedures see the same data without arguments. equivalence makes variables share the same storage. Modules replace both in new code, and the Fortran 2018 standard lists
them, block data and labelled do as obsolescent. sequence in a derived type fixes the order of its components in storage so it may appear in common. A procedure’s local variables are static in many FORTRAN 77 compilers and
automatic in Fortran 90 and later unless save is given or the procedure is recursive by default; save with no list keeps them all.
Parameterised derived types, enumerations and the newest forms
type :: matrix(k, n) ! Fortran 2003: type parameters
integer, kind :: k = kind(0.0)
integer, len :: n
real(k) :: a(n, n)
end type
enum, bind(c) ! Fortran 2003: C-compatible enumeration
enumerator :: red = 1, green, blue
end enum
enumeration type :: color ! Fortran 2023: an enumeration type of its own
enumerator :: red, green, blue
end enumeration type
typeof(x) :: y ! Fortran 2023: the same type as x
classof(x), allocatable :: z
typeof and classof type specifiers.Declaration extensions
- DEC structures:
structure /name/ … end structure,record /name/ var,union … map … end map … end union; a component is selected with.in DEC Fortran (-fdec-structurein GNU Fortran) - Cray pointers:
pointer (ptr, pointee) - Size forms:
integer*2,real*8,complex*16,logical*1,character*20(the last is the standard but obsolescentcharacter*form) - Statement functions (
f(x) = x*x + 1) are in FORTRAN 77 and obsolescent from Fortran 95: the viewer reads them as assignments - Typed constants in DEC style:
integer, parameter :: m = '1F'x