Using module variables as dimensions in function arguments

Actually it can check the dimensions too, since the caller knows the size of arrays, and the size “n” is passed as an argument. My understanding is that the two approaches are equivalent in terms of what the compiler can check.

Well, I meant that it can not check if the actual array arguments have sizes that are consistent with the dummy argument declarations, as it seemed to me that it was the point of @certik

I agree with this. When the programmer uses assumed size or explicit shape dummy declarations, he is telling the compiler that it is his responsibility to ensure that the actual and dummy array sizes meet the requirements. With f77, and particularly with individual routines compiled separately, that was always the programmer’s responsibility, so that is the convention that was carried over to f90 and later for those same declarations. If the programmer wants other consistency checks, then he must use explicit interfaces and other calling conventions, such as assumed shape. Also, allocatable and pointer dummy arguments even carry their array bounds into the subroutine (not just the shape), so there is a wide range of possibilities, and sometimes it is difficult to keep them all straight.

I think there are some corner cases when it might be difficult to check the dimensions, but in the default case of importing a function from a module, the compiler can check that actual and dummy arguments meet the requirements. If the programmer doesn’t want this check, that’s a separate issue.

  • variables appearing in the specification expression of an explicit shape array can be dummy argument variables, module variables, or local variables of parent scope (by host association), but can not be a local variable of the current procedure (because a local variable is not initialized at the specification point. Gfortran give compiling errors when you do this).

  • If a dummy array is declared as an explicit shape array, rank mismatch between actual array and dummy array is allowed. You get no warning even if turns on all compile warnings and have an explicit interface for the procedure. Because this is a feature of explicit shape arrays, not a weakness. If you want to avoid this kind of mismatch, use assumed-shape array instead.