模块何时超出 Fortran 90/95 的范围? [英] When does a module go out of scope in Fortran 90/95?

查看:16
本文介绍了模块何时超出 Fortran 90/95 的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的预期用途是

program main
  use mod

  external sub

  call sub
end program main

subroutine sub
  ! code here calls subroutines in mod
end subroutine sub

具体来说,module mod 会在subroutine sub 的范围内吗?另外,我有兴趣更广泛地了解模块何时处于/超出范围.如果重要的话,我正在使用 gfortran 4.6.1.

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.

推荐答案

它不在子程序 sub 的范围内,因为 sub 不能调用程序或使用来自 mod 的变量,因为 sub 不是子程序的一部分程序 main.它们没有任何共同点,是独立的编译单元,并且只能相互调用(如果它们是可调用的).

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).

考虑一下:

program main

  external sub

  call sub
end program main

subroutine sub
  use mod
  ! code here calls subroutines in mod
end subroutine sub

在这里,您可以在sub 中使用来自mod 的变量和例程,因为sub 明确使用了mod.

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

另一个例子,其中submain的内部过程:

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

此外,在这种情况下,您可以在 sub 中使用 mod 中的内容,因为 main 中的所有内容都在 sub.

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

最后,在这种情况下 mod 不在范围内,它类似于原始情况.

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

另一个问题是模块变量超出范围时未定义.Fortran 2008 通过使所有模块变量隐式save 解决了这个问题.

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.

这篇关于模块何时超出 Fortran 90/95 的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆