### 6.1.1.1 Adjustable Arrays

Adjustable arrays are dummy arguments in subprograms. The dimensions of an adjustable array are determined in the reference to the subprogram. The array declarator for an adjustable array can contain integer variables that are either dummy arguments or variables in a common block.

When the subprogram is entered, each dummy argument used in the array declarator must be associated with an actual argument, and each variable in a common block used in an array declarator must have a defined value. The dimension declarator is evaluated using the values of the actual arguments, variables in common blocks, and constants specified in the array declarator.

Argument association is not retained between one reference to a subprogram and the next reference to that subprogram.

The size of the adjustable array must be less than or equal to the size of the array that is its corresponding actual argument.

You can also declare adjustable arrays in RECORD statements not contained within structure declarations.

To avoid possible errors in subscript evaluation, make sure that the bounds expressions used to declare multidimensional adjustable arrays match the bounds as declared by the caller.

Examples

The following examples show valid and invalid adjustable arrays:

Valid

In the first example, the function computes the sum of the elements of a two-dimensional array. Notice how the dummy arguments M and N control the iteration:

          FUNCTION SUM(A,M,N)

          DIMENSION A(M,N)

          SUM = 0.0

          DO 10 J=1,N

          DO 10 I=1,M

    10    SUM = SUM + A(I,J)

          RETURN

          END

The following statements are sample calls on SUM:

    DIMENSION A1(10,35), A2(3,56)

    SUM1 = SUM(A1,10,35)

    SUM2 = SUM(A2,3,56)

    SUM3 = SUM(A1,10,10)

An adjustable array is undefined if a dummy argument array is not currently associated with an actual argument array. It is also undefined if any of the variables in the adjustable array declarator are either not currently associated with an actual argument or not in a common block.

The subroutine subprogram in the next example includes statements of a calling program unit. It illustrates how argument association is not retained between one reference to a subprogram and the next reference.

    SUBROUTINE S(A,I,X)

    DIMENSION A(I)

    A(I) = X

    RETURN

    ENTRY S1(I,A,K,L)

    A(I) = A(I) + 1.0

    RETURN

    END

The following program unit calls the subroutine subprogram from the previous example. This calling program unit defines B as a real array with 10 elements. The first call to subroutine S sets array element B(2) equal to the value 3.0. The second call to subroutine S (at entry point S1) increments array element B(5) by the value 1.0.

    DIMENSION B(10)

     . . .

    CALL S(B,2,3.0)

     . . .

    CALL S1(5,B,3,2)

The upper-dimension and lower-dimension bound values are determined once each time a subprogram is entered. These values do not change during the execution of that subprogram even if the values of variables contained in the array declaration are changed. In the next example, the adjustable array X is declared as X(-4:4,5) on entry to subroutine SUB. The assignments to I and J do not affect that declaration.

    DIMENSION ARRAY(9,5)

    L = 9

    M = 5

    CALL SUB(ARRAY,L,M)

    END

    SUBROUTINE SUB(X,I,J)

    DIMENSION X(-I/2:I/2,J)

    X(I/2,J) = 999

    J = 1

    I = 2

    END

On Alpha processors, the following form is also allowed for an adjustable array:

    SUBROUTINE SUB1(A,B)

    DIMENSION A(B(1))       ! An array element is allowed

A and B must be dummy arguments or B can be in COMMON.

Invalid

The following example is invalid; once a variable is used in an array declarator for an adjustable array, it must not appear in a type declaration that changes the variable's data type.

    SUBROUTINE SUB1(A,X)

    DIMENSION A(X)

    REAL X

Previous Page Next Page Table of Contents