Fortran 95 language features

Included are the additional features of TR-15581:Enhanced Data Type Facilities, which have been universally implemented.

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.

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"): There are at least two real kinds – the default and one with greater precision (this replaces DOUBLE PRECISION).

COMPLEX data type is built of two integer or real components: There are only two basic values of logical constants: .TRUE.

and the KIND function operates as expected: 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.

These functions are important for portable numerical software: 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).

DIMENSION keyword is optional and considered an attribute; if omitted, the array shape must be specified after array-variable name.

A derived data type may, of course, contain array components: so that Variables can be given initial values as specified in a specification statement: and a default initial value can be given to the component of a derived data type: When local variables are initialized within a procedure they implicitly acquire the SAVE attribute: This declaration is equivalent to for local variables within a subroutine or function.

It is also the only way to initialise just parts of such objects, as well as to initialise to binary, octal or hexadecimal values: The values used in DATA and PARAMETER statements, or with these attributes, are constant expressions that may include references to: array and structure constructors, elemental intrinsic functions with integer or character arguments and results, and the six transformational functions REPEAT, SELECTED_INT_KIND, TRIM, SELECTED_REAL_KIND, RESHAPE and TRANSFER (see Intrinsic procedures): It is possible to specify details of variables using any non-constant, scalar, integer expression that may also include inquiry function references: The usual arithmetic operators are available – +, -, *, /, ** (given here in increasing order of precedence).

Parentheses are used to indicate the order of evaluation where necessary: The rules for scalar numeric expressions and assignments accommodate the non-default kinds.

Note that in order to handle the error condition that arises when the two strings together exceed the preset 80-character limit, it would be safer to use a subroutine to perform the concatenation (in this case operator-overloading would not be applicable.)

The simple conditional test is the IF statement: IF (a > b) x = y A full-blown IF construct is illustrated by The CASE construct is a replacement for the computed GOTO, but is better structured and does not require the use of statement labels: Each CASE selector list may contain a list and/or range of integers, character or logical constants, whose values may not overlap within or between selectors: A default is available: There is only one evaluation, and only one match.

An example of a main (and complete) program is An example of a main program and an external subprogram, forming an executable program, is The form of a function is The form of reference of a function is x = name(a, b) An internal subprogram is one contained in another (at a maximum of one level of nesting) and provides a replacement for the statement function: We say that outer is the host of inner, and that inner obtains access to entities in outer by host association (e.g. to x), whereas y is a local variable to inner.

It has options to resolve name clashes if an imported name is the same as a local one: or to restrict the used entities to a specified set: These may be combined: We may specify the intent of dummy arguments: Also, INOUT is possible: here the actual argument must be a variable (unlike the default case where it may be a constant).

It is a copy of the header, specifications and END statement of the procedure concerned, either placed in a module or inserted directly: An explicit interface is obligatory for It allows full checks at compile time between actual and dummy arguments.

For We might have and to integrate f(x, y) over a rectangle: Direct recursion is when a procedure calls itself, as in Here, we note the RESULT clause and termination test.

In the FORALL statement and construct, any side effects in a function can impede optimization on a parallel processor – the order of execution of the assignments could affect the results.

Array handling is included in Fortran for two main reasons: At the same time, major extensions of the functionality in this area have been added.

A zero-sized array is handled by Fortran as a legitimate object, without special coding by the programmer.

A partial replacement for the uses to which EQUIVALENCE was put is provided by this facility, useful for local, temporary arrays, as in The actual storage is typically maintained on a stack.

To free dead storage we write, for instance, Deallocation of arrays is automatic when they go out of scope.

Given the matrix manipulation y = B C z, we can write the following code (although, in this case, the same result could be achieved more simply by other means): For objects of derived type we have to distinguish between pointer and normal assignment.

However, we can achieve the same effect by defining a derived data type with a pointer as its sole component: and then defining arrays of this data type where the storage for the rows can be allocated by, for instance, The array assignment s = tis then equivalent to the pointer assignments s(i)%r => t(i)%r for all components.

The intrinsic procedures are grouped into four categories: The procedures not already introduced are Bit inquiry Bit manipulation Transfer function, as in (replaces part of EQUIVALENCE) Subroutines These examples illustrate various forms of I/O lists with some simple formats (see below): Variables, but not expressions, are equally valid in input statements using the READ statement: If an array appears as an item, it is treated as if the elements were specified in array element order.

The format specification may also be given in the form of a character expression: or as an asterisk – this is a type of I/O known as list-directed I/O (see below), in which the format is defined by the computer system: Input/output operations are used to transfer data between the storage of an executing program and an external medium, specified by a unit number.

The value may be any nonnegative integer allowed by the system for this purpose (but 0, 5 and 6 often denote the error, keyboard and terminal, respectively).

An asterisk is a variant – again from the keyboard: A read with a unit specifier allows exception handling: There a second type of formatted output statement, the WRITE statement: These allow format conversion between various representations to be carried out by the program in a storage area defined within the program itself.

Another variant permits an inquiry about the length of an output list when used to write an unformatted record.

Other specifiers are IOSTAT, OPENED, NUMBER, NAMED, FORMATTED, POSITION, ACTION, READ, WRITE, READWRITE.