SIZE(ARRAY, DIM, KIND)

Purpose

Returns the extent of an array along a specified dimension or the total number of elements in the array.

Class

Inquiry function

Argument type and attributes

ARRAY
An array of any data typeTS begins or an assumed-rank objectTS ends. The corresponding actual argument must not be a scalar, disassociated pointer, or allocatable array that is not allocated. The actual argument can be an assumed-size array if DIM is present and has a value that is less than the rank of ARRAY.
DIM (optional)
An INTEGER scalar. Its value must be in the range 1 ≤ DIM ≤ RANK(ARRAY). TS beginsIt must not be present if ARRAY is an assumed-rank object that is associated with a scalar.TS ends
Fortran 2003 beginsKIND (optional)
An INTEGER scalar. Its value must be specified by a constant expression. Fortran 2003 ends

Result type and attributes

Result value

The result equals the extent of ARRAY along dimension DIM; or, if DIM is not specified, it is the total number of array elements in ARRAY.

TS begins
  • If ARRAY is an assumed-rank object that is associated with a scalar, the result is 1.
  • If ARRAY is an assumed-rank object that is associated with an assumed-size array, and
    • If DIM is present and equal to the rank of ARRAY, the result is -1.
    • If DIM is not present, the result is a negative value that is equal to PRODUCT([(SIZE(ARRAY, I, KIND), I=1, RANK(ARRAY))]).
TS ends

Examples

! A is the array  | 1 -4  7 -10 |
!                 | 2  5 -8  11 |
!                 | 3  6  9 -12 |

       RES = SIZE( A )
! The result is 12 because there are 12 elements in A.

       RES = SIZE( A, DIM = 1)
! The result is 3 because there are 3 rows in A.

       RES = SIZE( A, DIM = 2)
! The result is 4 because there are 4 columns in A.