Is passing explicit-shape arrays to procedures discouraged?

According to the Fortran best practice docs, the preferred way to pass arrays to procedures is as assumed-shape arrays. I prefer to pass explicit-shape arrays to procedures. Just want to ask if that is OK.

1 Like

@fortran4r , you may want to review this thread again closely, you will notice a longish discussion there on dummy arguments that are explicit-shape arrays.

3 posts were merged into an existing topic: Size of long array

I think the main gotcha is that explicit shape dummy arrays must be contiguous. That means if the actual argument is not contiguous, then copy-in/copy-out is invoked by the compiler, using a temporary intermediate array. If that occurs in a tight loop that is executed many times, then the performance will be very poor. If it only happens rarely, then you won’t notice it. On the other hand, if the dummy argument is assumed-shape, then it can be associated directly with the noncontiguous actual argument, with no memory allocation and no copying of intermediate work arrays required. This applies to routines with an implicit interface and also to explicit interfaces with explicit shape dummy arguments. Several established math libraries have this problem, including LAPACK.

1 Like

The biggest reason I avoid using explicit shape procedure arguments is that nothing actually checks that the dummy and actual arguments are actually the same shape. Off-by-one errors, mismatched arguments (especially in the case of 2-d arrays), etc. go uncaught by the compiler and (if you’re lucky) are caught only at run-time or (if you’re unlucky) simply lead to erroneous answers as unintended memory accesses occur.

1 Like