|Fortran Language Reference Manual, Volume 1 - S-3692-51|
|Prev Section||Chapter 2. Fortran Concepts and Terms||Next Section|
Before a calculation can be performed, its data environment must be developed. The data environment consists of data objects that possess certain properties, attributes, and values. The steps in a computational process generally specify operations that are performed on operands (or objects) to create desired results or values. Operands can be constants, variables, constructors, or function references; each has a data type and (if defined) a value. In some cases the type can be assumed by the processor; in other cases it may be declared. A data object has attributes other than type. Chapter 4, discusses data type in detail; Chapter 5, discusses the other attributes of program entities; and Chapter 6, and Chapter 7, describe how data objects are used.
The Fortran language provides five intrinsic data types: real, integer, complex, logical, and character, and it lets you define additional types. Sometimes it is natural to organize data in combinations consisting of more than one type. For example, assume that a program is written to monitor the patients in a hospital. For each patient, certain information must be maintained, such as the patient's name, room number, temperature, pulse rate, medication, and prognosis for recovery. Because all of this data describes one object (a particular patient), it would be convenient to have a means to refer to the aggregation of data by a single name. In Fortran, an aggregation of data values of different types is called a structure. To use a structure, a programmer must first define the type of the structure. After the new type is defined, any number of structures of that type can be declared. This mechanism might seem slightly cumbersome if only one such structure is needed in a program, but usually several are needed. The following is an example of a user-defined type with three components:
After type PATIENT is defined, objects (structures) of the type can be declared. For example:
TYPE(PATIENT) JOHN_JONES, SALLY_SMITH
Some intrinsic types have more than one representation (or kind). Fortran provides both single- and double-precision representations of the real and complex types; chapter 4 describes these data representations in detail. Fortran permits more than one representation for the integer, logical, and character types. The Cray Fortran Compiler supports more than one representation for the integer and logical types, but it supports only a single character type (ASCII). Alternative representations for the integer type permit different ranges of integers. Also, Fortran provides representations for most intrinsic data types that interoperate with C functions.
Single objects, whether intrinsic or user-defined, are scalar. Even though a structure has components, it is considered to be a scalar. A set of scalar objects, all of the same type, may be arranged in patterns involving columns, rows, planes, and higher-dimensioned configurations to form arrays. It is possible to have arrays of structures. An array may have a maximum of seven dimensions. The number of dimensions is called the rank of the array. It is declared when the array is declared and cannot change. The size of the array is the total number of elements and is equal to the product of the extents in each dimension. The shape of an array is determined by its rank and its extents in each dimension. Two arrays that have the same shape are said to be conformable. The following are examples of array declarations:
REAL COORDINATES(100, 100) INTEGER DISTANCES(50) TYPE(PATIENT) MATERNITY_WARD(20)
In Fortran, an array is treated as an object and is allowed to appear in an expression or be returned as a function result. Intrinsic operations involving arrays of the same shape are performed element-by-element to produce an array result of the same shape. There is no implied order in which the element-by-element operations are performed.
A portion of an array, such as an element or section, can be referenced as a data object. An array element is a single element of the array and is scalar. An array section is a subset of the elements of the array and is itself an array.
Data objects in Fortran can be declared to have the POINTER attribute. Pointer objects must be associated with a target before they can be used in any calculation. This is accomplished by allocation of the space for the target or by assignment of the pointer to an existing target. The association of a pointer with a target can change dynamically as a program is executed. If the pointer object is an array, its size and shape could change dynamically, but its rank is fixed by its declaration. The following is an example of pointer array declaration and allocation:
REAL, POINTER :: LENGTHS(:) ALLOCATE(LENGTHS (200))
Following execution of an ALLOCATE statement, LENGTHS points at (is associated with) an array with 200 elements. The elements as yet have no values.
Data objects can have the ALLOCATABLE attribute. Space must be allocated for the data object before it can be used in any calculation. An allocatable array can be deallocated and reallocated with a different size as the program executes. The size and shape can change at each allocation, but the rank is fixed by the declaration. The following is an example of allocatable array declaration and allocation:
REAL, ALLOCATABLE :: LENGTHS(:) ALLOCATE (LENGTHS(200))
The similarities of these examples reflect the similarity of some of the uses of allocatable variables and pointers, but pointers have more functionality. Pointers can be used to create dynamic data structures, such as linked lists and trees. The target of a pointer can be changed by reallocation or pointer assignment. The extents of an allocatable array can be changed only by deallocating and reallocating the array. If the values of the elements of an allocatable array are to be preserved, a new array must be allocated and the values moved to the new array before the old array is deallocated.
An automatic data object can be an array or a character string. Automatic objects can be declared in a subprogram. These local data objects are created on entry to the subprogram and disappear when the execution of the subprogram completes. They are useful in subprograms for temporary arrays and characters strings whose sizes are different for each reference to the subprogram. The following is an example of a subprogram unit with automatic array TEMP:
SUBROUTINE SWAP_ARRAYS(A, B) REAL, DIMENSION(:) :: A, B REAL, DIMENSION(SIZE(A)) :: TEMP TEMP = A A = B B = TEMP END SUBROUTINE SWAP_ARRAYS
A and B are assumed-shape array arguments; that is, they take on the shape of the actual argument. TEMP is an automatic array that is created the same size as A on entry to subroutine SWAP_ARRAYS. SIZE(3i) is an intrinsic function that is permitted in a declaration statement.