My intended use is
program main
use mod
external sub
call sub
end program main
subroutine sub
! code here calls subroutines in mod
end subroutine sub
Specifically, will module mod
be in scope in subroutine sub
? Also, I'd be interested to know more generally when a module is in/out of scope. I'm using gfortran 4.6.1, if it matters.
It is not in scope of subroutine sub, as sub cannot call routines or use variables from mod, because
sub
is not part of the programmain
. They have nothing in common, are separate compilation units and only may call each other (if they are callable).Consider this:
Here, you can use variables and routines from
mod
insub
, becausesub
explicitly usesmod
.Another example, where
sub
is an internal procedure ofmain
:Also in this case you can use things from
mod
insub
because everything frommain
is in scope insub
.Finally, in this case
mod
is not in scope, it is similar to the original case.Another issue is the undefining of module variables, when they go out of scope. Fortran 2008 solved this by making all module variables implicitly
save
.