I'm trying to decide which one of these two options would be the best:
subroutine sqtrace( Msize, Matrix, Value )
integer, intent(in) :: Msize
real*8, intent(in) :: Matrix(Msize, Msize)
real*8, intent(out) :: Value
[instructions...]
end subroutine sqtrace
VS
subroutine sqtrace( Matrix, Value )
real*8, intent(in) :: Matrix(:,:)
real*8, intent(out) :: Value
if ( size(Matrix,1) /= size(Matrix,2) ) then
[error case instructions]
end if
[instructions...]
end subroutine sqtrace
I understand that when you compile with warnings, the first case should automatically check at compile time if calls to sqtrace
comply with the size indicated. However, I don't know if the compiler can perform those checks when the given arguments are allocatable, for example (more so if such allocation depends on other things that are determined at runtime). The second one requires an explicit interface and has more code (the checks), but would seem to catch more errors.
Which are the advantages/disadvantages of using each and in which cases should one go with one over the other?
First, some terminology. Consider the dummy arguments declared as
(a deferred shape array may also be a pointer rather than an allocatable).
I won't answer in terms of which is better in a given situation, but will simply detail some of the important characeristics leaving choice, where there is one, to the programmer. Crudely, many view explicit shape arrays as "Fortran 77" and assumed shape arrays as "Fortran 90+".
Shape:
Contiguousness:
Restrictions on actual argument:
Interfaces in the calling scope:
Consider
real a(6)
. This may be an actual argument to the dummiesa(1::2)
may be associated withb
but asb
is contiguous copy-in/copy-out will be involved. Copy-in/copy-out needn't be involved when associated withd
, but it may be.There are plenty of other aspects, but hopefully this is an initial high-level introduction.