There have been several discussions about allowing for variables to be
initialized in the declarations at the time of the call of the procedure,
instead of the current situation where they are essentially initialized
at program initialization and then SAVED (assuming they are variables
and not parameter constants). But I do not remember anyone proposing
anything exactly like this. People coming to Fortran with previous C experience in
particular argue for something similiar where
subroutine A(n)
integer :: i=10*n
end subroutine A
would set “i” on entry and allow for variables in the initialization, more like C; instead of acting more like a FORTRAN DATA statement and requiring constants.
Note that as well as an allocatable array (as you mentioned) you can pass
the dimensions as arguments, call a routine that declares the values
and then calls another routine (which could be a CONTAINED routine,
or use BLOCK and ASSOCIATE. Not exactly the same, but somewhat in the
same spirit. Might be others, but these are the closest standard methods
that came to mind that are somewhat similar …
Using BLOCK …
subroutine my_sub(a,w)
real,intent(in) :: a(:)
real,intent(out) :: w
integer :: m,n
m=size(a)*2
n=7*size(a)**2
block
real :: e(m), f(n)
w = size(e)+size(f)
end block
end subroutine my_sub
Using ASSOCIATE and BLOCK …
subroutine my_sub(a,w)
real,intent(in) :: a(:)
real,intent(out) :: w
associate ( m=>size(a)*2, n=>7*size(a)**2)
block
real :: e(m), f(n)
w = size(e)+size(f)
end block
end associate
end subroutine my_sub
CONTAINED …
module m_subs
contains
subroutine my_sub(a,w)
implicit none
real,intent(in) :: a(:)
real,intent(out) :: w
call real_my_sub( m = size(a)*2, n = 7*size(a)**2 )
contains
subroutine real_my_sub(m,n)
integer :: m,n
real :: e(m), f(n)
w = size(e)+size(f)
end subroutine real_my_sub
end subroutine my_sub
end module m_subs
program testit
use m_subs, only : my_sub
implicit none
real :: a(10)
real :: w
call my_sub(a,w)
write(*,*)w
end program testit
I would not rate any of those highly intuitive or succinct; but they do something similar.