Variables [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Reference
Variables
A variable name is a symbolic name that represents a data element whose
value can be changed during program execution by the use of assignment
statements, READ statements, and so forth.
A variable can represent a single value of one simple type, such as
character, complex, integer, logical, or real; a collection of values of
the same type, as in an array; or a collection of values of different
types, as in a record.
Refer to "Symbolic Names" earlier in this chapter for a description
of valid variable names.
Simple Variables
A simple variable is used to process a single data item. It identifies a
storage area that can contain only one value at a time. Subscripted
variables are treated in this manual as simple variables unless stated
otherwise.
Examples
total
voltage
Final_Score
i
sum_of_values
ERROR_FLAG1
array3_element(i,j)
FORMAT
Arrays
An array is a collection of several values of the same type. An array
name is a symbolic name that represents all values or elements of an
array. To designate exactly one element of the array, follow the array
name with one or more subscripts.
A group of values arranged in a single row is a one-dimensional array.
The elements of such an array are identified by a single subscript. If
two subscripts are used to identify an element of an array, then that
array is two-dimensional, and so forth. An array can have an unlimited
number of dimensions. The number of dimensions allowed in an array is
system dependent.
Array Declarators.
Array declarators are used in DIMENSION, COMMON, VIRTUAL, and type
declaration statements to define the number of dimensions, the number of
elements per dimension (called bounds), and the data to be stored in the
elements.
Syntax
name ( d [, d][...] )
-----------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| name | Symbolic name of the array. | None. |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| d | Dimension declarator. | There must be one dimension |
| | | declarator for each dimension of |
| | | the array. |
| | | |
-----------------------------------------------------------------------------------------------
Examples Notes
--------------------------------------------------------------------------------------
DIMENSION xyz(4,2,4) Three-dimensional REAL array of xyz with 32 elements.
COMMON iabc(3,4) Two-dimensional INTEGER*4 array of iabc with 12 elements.
INTEGER*2 I2(4) One-dimensional INTEGER*2 array of I2 with 4 elements.
The syntax of a dimension declarator is:
Syntax
[m :] n
-----------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| m | Lower dimension bound. | None. |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| n | Upper dimension bound. | The upper bound must be greater |
| | | than or equal to the lower bound. |
| | | |
-----------------------------------------------------------------------------------------------
If only the upper dimension bound is specified, the value of the lower
dimension bound is one. The value of either dimension bound can be
positive, negative, or zero; however, the value of the upper dimension
bound must be greater than or equal to the value of the lower dimension
bound.
The lower and upper dimension bounds are arithmetic expressions
containing constants, symbolic names of constants, or variables. The
expressions defining the upper and lower bounds must not contain a
function or array element reference. The upper dimension bound of the
last dimension in the array declarator of a formal argument can be an
asterisk, signifying that the last dimension is assumed (undefined).
NOTE Using an asterisk in a dimension declarator is limited to
declarators of formal arguments of subprograms.
The array bounds indicate the number of dimensions of the array and the
maximum number of elements in each dimension. The number of elements in
each dimension is defined by n - m + 1, where n is the upper bound and m
is the lower bound.
Examples Notes
---------------------------------------------------------------------------------------
name(4,-5:5,6) Specifies a three-dimensional array. The
first dimension can have four elements, the
second 11, and the third six.
decision_table (2,3,2,2,3,4,2) Specifies a seven-dimensional array.
m(0:0) Specifies a one-dimensional array of one
element: m(0).
list(10) Specifies a one-dimensional array of 10
elements: list(1) to list(10).
A complete array declarator for a particular array can be used once only
in a program unit, although the array name can appear in several
specification statements. For example, if the array declarator is used
in a DIMENSION statement, the array name can only be used in a COMMON or
type statement. If the complete array declarator is used in a COMMON or
type statement, the array must not be mentioned in a DIMENSION statement.
Normally, array bounds are specified with integer constants. If the
bounds are specified with integer variables, the integer variables must
be formal arguments to the subprogram. However, the array itself can
either be a formal argument or a nonstatic local variable (that is, one
that does not appear in a SAVE or DATA statement). See "Adjustable
Arrays" and "Dynamic Arrays" in the following sections for further
information.
Adjustable Arrays.
Normally, array bounds are specified by integer constants and are
determined by the values of these constants. In an adjustable array, one
or more of the array bounds are specified by an expression involving
integer variables instead of integer constants.
Adjustable arrays can be used in subprograms to allow the array bound to
be defined as a value passed from the caller of the subprogram. The
array bounds are therefore formal arguments, and storage is allocated for
the array by the caller of the subprogram in which the array is found.
The next example illustrates an adjustable array.
Examples Notes
--------------------------------------------------------------------------------------
PROGRAM main Storage is allocated for array by the program main. The
INTEGER array(10) subprogram routine uses the variable i only for bounds
i = 10 checking and subscript calculation.
CALL routine(array,i)
END
SUBROUTINE routine(ar,i)
INTEGER ar(i)
ar(1) = i
END
Dynamic Arrays.
An array that is a nonstatic local variable is called a dynamic array.
For a dynamic array, storage is allocated by the current subprogram
dynamically on the stack. This dynamic array feature is an HP extension
to the ANSI 77 standard. The next example illustrates the use of a
dynamic array.
Examples Notes
--------------------------------------------------------------------------------------
PROGRAM main main passes only the integer i to routine. dyn_array is
i = 10 a nonstatic local array. The subprogram allocates
CALL routine(i) storage for 10 4-byte integers on the stack for
END dyn_array.
SUBROUTINE routine(i)
INTEGER dyn_array(i)
dyn_array(i) = i
END
Subscripts.
Subscripts designate a specific element of an array. An array element
reference (subscripted variable) must contain the array name followed by
as many subscripts as there are dimensions in the array. The subscripts
are separated by commas and enclosed in parentheses. Each subscript
value must fall between the declared lower and upper bounds for that
dimension.
For example, a subscripted variable for a one-dimensional array of three
elements declared by a(3) or a(1:3) could have the form a(1), a(2), or
a(3) to represent the elements of the array a. If a subscript is outside
its declared lower and upper bounds, the results are unpredictable; the
compiler does not generate an error message (unless the RANGE option is
specified).
Examples Notes
--------------------------------------------------------------------------------------
arr(1,2) Represents the element 1,2 of the array arr. If arr was
declared by arr(10,20), arr would describe a
two-dimensional table and arr(1,2) would describe the
element in the second column of the first row.
chess_board(i,j,k) Subscripts i, j, and k are variables that represent
different elements of array chess_board.
arr(i+4,j-2) Subscripts i+4 and j-2 are expressions that represent
specific elements of array arr when evaluated.
Array Element Storage.
The total number of elements in an array is calculated by multiplying the
number of elements in each dimension. For example, the array declarator
i(3,4,-3:5) indicates that array i contains 108 elements:
3*4*(5-(-3)+1) = (3*4*9) = 108
The amount of memory needed to store an array is determined by the number
of elements in the array and the type of data that the array contains.
LOGICAL*1 arrays store each element in one byte.
INTEGER*2 and LOGICAL*2 arrays store each element in two bytes.
INTEGER*4, REAL*4, and LOGICAL*4 arrays store each element in four
bytes.
REAL*8 and COMPLEX*8 arrays store each element in eight bytes.
REAL*16 and COMPLEX*16 arrays store each element in 16 bytes.
CHARACTER arrays store each character in one byte.
A one-dimensional array is stored as a linear list. Arrays of higher
dimensions are stored in "column major order", with the first
subscriptvarying most rapidly, the second the next most rapidly, and so
forth, with the last varying least rapidly.
Example
Array declarator: arr(2,0:1,-5:-4)
Array storage: arr(1,0,-5)
arr(2,0,-5)
arr(1,1,-5)
arr(2,1,-5)
arr(1,0,-4)
arr(2,0,-4)
arr(1,1,-4)
arr(2,1,-4)
Arrays as Parameters.
When arrays are passed as parameters, the size of the actual argument
array must not exceed the size of the formal argument array. Because
array bounds across separate compilation units are not checked at
run-time, no warning is issued if the actual array size exceeds the
formal array size. Altering these unreserved locations could yield
unpredictable results.
Character Substrings
A character substring is a contiguous portion of a character variable.
Syntax
name ( [first] : [last] )
array ( s [, s][...] ) ( [first] : [last] )
-----------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| name | Character variable name. | None. |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| array(s[,s][...]) | Character array element. | None. |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| first | Integer expression that specifies | Default value is one. |
| | the leftmost position of the | |
| | substring. | |
| | | |
-----------------------------------------------------------------------------------------------
| | | |
| last | Integer expression that specifies | Default value is the length of the |
| | the rightmost position of the | string. |
| | substring. | |
| | | |
-----------------------------------------------------------------------------------------------
The value of first and last must be such that:
1 <= first <= last <= len
where len is the length of the character variable, named constant, or
array element. The length of a substring is last - first + 1.
Examples Notes
--------------------------------------------------------------------------------------
name(2:4) If the value of name is 'SUSANNA', then name(2:4)
specifies 'USA'.
address(:4) If the value of address is '1452 NORTH', then address(:4)
specifies '1452'.
city(6,2) (5:8) If the value of city(6,2) is 'SAN JOSE', then city(6,2)
(5:8) specifies 'JOSE'.
title or title(:) These specify the complete character variable.
MPE/iX 5.0 Documentation