Passing size as argument VS assuming shape in Fortran procedures

First, some terminology. Consider the dummy arguments declared as

real :: a(n)                ! An explicit shape array
real :: b(:)                ! An assumed shape array
real, allocatable :: c(:)   ! A deferred shape array (allocatable)
real, pointer :: d(:)       ! A deferred shape array (pointer)
real :: e(*)                ! An assumed size array
real :: f(..)               ! An assumed rank array/scalar

I won’t answer in terms of which is better in a given situation, but will simply detail some of the important characteristics leaving choice, where there is one, to the programmer. Crudely (and incorrectly), many view explicit shape arrays as “Fortran 77” and assumed and deferred shape arrays as “Fortran 90+”.

Assumed size and assumed rank arguments are beyond the scope of this question and answer.


Shape:

  • the shape of an explicit shape array follows its declaration;
  • the shape of an assumed shape array dummy argument is that of the actual argument;
  • the shape of a deferred shape dummy argument may be undefined, becoming defined in the procedure, or that of the actual argument.

Contiguousness:

  • an explicit shape array is simply contiguous;
  • an assumed shape array dummy argument’s contiguousness relates to that of the associated actual argument;
  • a deferred shape dummy argument may be that of the actual argument, or depending on the procedure’s execution.

Restrictions on actual argument:

  • an actual argument associated with an explicit shape array must have at least as many elements as the dummy argument;
  • an actual argument associated with an assumed shape array must not itself be assumed size;
  • an actual argument associated with an assumed or deferred shape array must be of the same rank as the dummy argument.

Interfaces in the calling scope:

  • if a dummy argument is of assumed or deferred shape, the referencing scope must have accessible an explicit interface for the procedure.

Consider real a(6). This may be an actual argument to the dummies

real b(3)
real c(2,3)
real d(:)   ! With an explicit interface available

a(1::2) may be associated with b but as b is contiguous copy-in/copy-out will be involved. Copy-in/copy-out needn’t be involved when associated with d, but it may be.

There are plenty of other aspects, but hopefully this is an initial high-level introduction.

Leave a Comment