When does a module go out of scope in Fortran 90/95?

后端 未结 1 757
不知归路
不知归路 2020-12-11 23:42

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          


        
相关标签:
1条回答
  • 2020-12-12 00:01

    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 program main. They have nothing in common, are separate compilation units and only may call each other (if they are callable).

    Consider this:

    program main
    
      external sub
    
      call sub
    end program main
    
    subroutine sub
      use mod
      ! code here calls subroutines in mod
    end subroutine sub
    

    Here, you can use variables and routines from mod in sub, because sub explicitly uses mod.

    Another example, where sub is an internal procedure of main:

    program main
      use mod
    
      call sub
    
      contains
    
        subroutine sub
          ! code here calls subroutines in mod
        end subroutine sub
    
    end program main
    

    Also in this case you can use things from mod in sub because everything from main is in scope in sub.

    Finally, in this case mod is not in scope, it is similar to the original case.

    program main
      use mod
      use mod2
    
      call sub
    end program main
    
    module mod2
    
      contains
    
        subroutine sub
    
          ! code here calls subroutines in mod
        end subroutine sub
    
    end module mod2
    

    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.

    0 讨论(0)
提交回复
热议问题