FORTRAN Reference Manual

Program Units
FORTRAN Reference Manual528615-001
4-12
Assumed-Size Array Declarator
PROGRAM MAIN
REAL result(100), avg
READ *, n
READ *, (result(j), j = 1,n)
CALL mean(n, result, avg)
PRINT *, avg
END
SUBROUTINE mean(n,data,average)
REAL data(100), average, sum
sum = 0
DO 10 j = 1,n
sum = sum + data(j)
10 CONTINUE
average = sum/n
END
The main program passes values for the number of students and test scores to the
subroutine, which accumulates the sum of the test scores, calculates their average,
and returns the value of AVERAGE to the calling program. Note that you must
dimension the associated dummy array DATA in the subprogram.
The requirement that associated dummy arrays be dimensioned in the subprogram
would restrict the availability of the subprogram to calling programs that pass arrays of
the same size as declared by the dummy argument. You can use one of the following
two methods to avoid explicitly declaring the array size in the subroutine:
Use an assumed-size array declarator in the subroutine
Use an adjustable array declarator in the subroutine
Assumed-Size Array Declarator
An assumed-size array declarator takes the form:
array-name( d [, d]... ,[ lower:]*)
where d is a dimension declarator in the form
[ lower:] upper
You can use an asterisk only for the upper dimension bound of the last dimension.