module - How to explicitely use inherited variables in Fortran? -
i have question regarding best practices of model/variable usage:
let's assume have module containing few variable/parameter definitions , subroutines use these variables.
i not need explicitly use these variables in subroutines since inherited parent module - better practice so?
example:
module test implicit none integer, parameter :: = 1 real :: x contains subroutine idk(y,z) real, intent(in) :: y real, intent(out) :: z if(a .eq. 1) z = x*y + 5. else z = x*y - 5. end if end subroutine idk end module test the above example should work fine better add
use test, only: a,x to declaration part of subroutine idk?
in reasoning, there 2 main points here:
1) pro: explicitly adding line let's me see variables needed in subroutine. in many cases, module contains quite number of variables few needed in each subroutine. reasons of better comprehensibility, beneficial add line.
but
2) contra: in quite few cases, 1 needs lot of variables/parameters declared above (sometimes numbering more 100 parameters). explicitly using these @ beginning of subroutine unnecessarily clutters code, reducing readability of code.
point 1 matters if few variables need included, whereas point 2 important if many variables need included. think silly 1 thing few variables , many - once have picked convention, should stick imho...
is there best practice regarding this?
addition: alternatively, 1 declare subroutine as
subroutine idk(b,w,y,z) and call idk(a,x,y,z).
on 1 hand, give me greater flexibility if later decide want use idk other variables.
on other hand, increases risk of mistakes if change later (say, realize don't need parameter a condition parameter c. in first cases, switch out a -> c in subroutine. in last case, need change every call idk(c,...). if there lot of these calls, prone mistakes)
i appreciate input! thank you!
there absolutely no reason use module being defined. illegal. may happen compile if module compiled before , compiler can find .mod file, file, other wrong.
you should expect error such as
ifort -c assoc.f90 assoc.f90(10): error #6928: module-name on use statement in program unit cannot name of encompassing scoping unit. [test] use test ------^ the module subroutine gets variables host module through host association , use statement use association. these 2 different things , should not mixed.
if want avoid global variables, pass them arguments. general advice. best depends on each case , programmer , cannot answered generally.
Comments
Post a Comment