This is an overview of Fortran 95 language features which is based upon the standards document which has been replaced by a newer version. Included are the additional features of TR-15581:Enhanced Data Type Facilities, which have been universally implemented. Old features that have been superseded by new ones are not described – few of those historic features are used in modern programs although most have been retained in the language to maintain backward compatibility. The additional features of subsequent standards, up to Fortran 2023, are described in the Fortran 2023 standard document, ISO/IEC 1539-1:2023. Some of its new features are still being implemented in compilers. Details can also be found in a range of textbooks, for instance and see the list at Fortran Resources. Sources for the description in the sections below can be found in the standards documents, textbooks as well as the Bibliography.
Language elements Fortran is case-insensitive. The convention of writing Fortran keywords in upper case and all other names in lower case is adopted in this article; except, by way of contrast, in the input/output descriptions (Data transfer and Operations on external files).
Basics The basic component of the Fortran language is its character set. Its members are
the letters A ... Z and a ... z (which are equivalent outside a character context) the numerals 0 ... 9 the underscore _ the special characters = : + blank - * / ( ) [ ] , . $ ' ! " % & ; < > ? Tokens that have a syntactic meaning to the compiler are built from those components. There are six classes of tokens:
From the tokens, statements are built. These can be coded using the new free source form which does not require positioning in a rigid column structure:
Note the trailing comments and the trailing continuation mark. There may be 39 continuation lines, and 132 characters per line. Blanks are significant. Where a token or character constant is split across two lines:
a leading & on the continued line is also required.
Intrinsic data types Fortran has five intrinsic data types: INTEGER, REAL, COMPLEX, LOGICAL and CHARACTER. Each of those types can be additionally characterized by a kind. Kind, basically, defines internal representation of the type: for the three numeric types, it defines the precision and range, and for the other two, the specifics of storage representation. Thus, it is an abstract concept which models the limits of data types' representation; it is expressed as a member of a set of whole numbers (e.g. it may be {1, 2, 4, 8} for integers, denoting bytes of storage), but those values are not specified by the Standard and not portable. For every type, there is a default kind, which is used if no kind is explicitly specified. For each intrinsic type, there is a corresponding form of literal constant. The numeric types INTEGER and REAL can only be signed (there is no concept of sign for type COMPLEX).
Literal constants and kinds
INTEGER Integer literal constants of the default kind take the form
Kind can be defined as a named constant. If the desired range is ±10kind, the portable syntax for defining the appropriate kind, two_bytes is
that allows subsequent definition of constants of the form
Here, two_bytes is the kind type parameter; it can also be an explicit default integer literal constant, like
but such use is non-portable. The KIND function supplies the value of a kind type parameter:
and the RANGE function supplies the actual decimal range (so the user must make the actual mapping to bytes):
Also, in DATA (initialization) statements, binary (B), octal (O) and hexadecimal (Z) constants may be used (often informally referred to as "BOZ constants"):
REAL There are at least two real kinds – the default and one with greater precision (this replaces DOUBLE PRECISION). SELECTED_REAL_KIND functions returns the kind number for desired range and precision; for at least 9 decimal digits of precision and a range of 10−99 to 1099, it can be specified as:
and literals subsequently specified as
Also, there are the intrinsic functions
that give in turn the kind type value, the actual precision (here at least 9), and the actual range (here at least 99).
COMPLEX COMPLEX data type is built of two integer or real components:
LOGICAL There are only two basic values of logical constants: .TRUE. and .FALSE.. Here, there may also be different kinds. Logicals don't have their own kind inquiry functions, but use the kinds specified for INTEGERs; default kind of LOGICAL is the same as of INTEGER.
and the KIND function operates as expected:
CHARACTER The forms of literal constants for CHARACTER data type are
(the last being an empty string). Different kinds are allowed (for example, to distinguish ASCII and UNICODE strings), but not widely supported by compilers. Again, the kind value is given by the KIND function:
Number model and intrinsic functions The numeric types are based on number models with associated inquiry functions (whose values are independent of the values of their arguments; arguments are used only to provide kind). These functions are important for portable numerical software:
Scalar variables Scalar variables corresponding to the five intrinsic types are specified as follows:
where the optional KIND parameter specifies a non-default kind, and the :: notation delimits the type and attributes from variable name(s) and their optional initial values, allowing full variable specification and initialization to be typed in one statement (in previous standards, attributes and initializers had to be declared in several statements). While it is not required in above examples (as there are no additional attributes and initialization), most Fortran-90 programmers acquire the habit to use it everywhere. LEN= specifier is applicable only to CHARACTERs and specifies the string length (replacing the older *len form). The explicit KIND= and LEN= specifiers are optional:
works just as well. There are some other interesting character features. Just as a substring as in
was previously possible, so now is the substring
Also, zero-length strings are allowed:
Finally, there is a set of intrinsic character functions, examples being
Derived data types For derived data types, the form of the type must be defined first:
and then, variables of that type can be defined:
To select components of a derived type, % qualifier is used:
… excerpt ends here. Continue reading the full article.