问题描述
如果在名为myCB
的公共块中有一个名为var
的变量,我可以使用相同的名称在其他两个不使用公共块myCB
的子例程之间传递参数吗?
If I have a variable called var
which is in a common block named myCB
may I use the same name to pass an argument between two other subroutines which are not using the common block myCB
?
代码如下.
Subroutine SR1(Var)
!something here using Var
end Subroutine SR1
Subroutine SR2()
....
Call SR1(B)
....
end Subroutine SR2
Subroutine SR3()
common \myCB\ Var
...
! something using the other Var shared with SR4
......
end Subroutine SR3
Subroutine SR4()
common \myCB\ Var
....
... ! something using the other Var shared with SR3
....
end Subroutine SR4
我确实在SR1
和SR2
之间传递Var
时遇到问题,问题可能出在公共块中的另一个名为Var
的问题上吗?
I do have problem with Var
passing between SR1
and SR2
, could the problem come from the other named Var
in the common block ?
推荐答案
如果您不想过多地修改旧代码库,建议您将common
块放在module
中,然后导入变量需要访问权限时:
If you don't want to modify the legacy code base too much, I suggest you put the common
block in a module
and import the variables when access is required:
module myCB_mod
common /myCB/ var, var2, var3
save ! This is not necessary in Fortran 2008+
end module myCB_mod
subroutine SR2()
use myCB_mod
!.......
call SR1(B)
!.....
end subroutine SR2
subroutine SR3()
use myCB_mod
!.......
end subroutine SR3
subroutine SR4()
use myCB_mod
!.....
end subroutine SR4
或更妙的是,我建议您完全避免使用common
块(这需要完全重写旧代码库),并将所有子例程限制在module
or better yet, I suggest you avoid common
blocks altogether (this requires a full rewrite of the legacy code base) and confine all your subroutines inside a module
module myCB
implicit none
real var, var2, var3
save ! This is not necessary in Fortran 2008+
end module myCB
module mySubs
use myCB
implicit none
contains
subroutine SR2()
!.......
call SR1(B)
!.....
end subroutine SR2
subroutine SR3()
!.......
end subroutine SR3
subroutine SR4()
!.....
end subroutine SR4
end module
最后,common
块中的变量是否需要初始化?如果是这样,这甚至会导致涉及data
语句甚至block data
构造的更多复杂情况.
Lastly, do the variables in your common
block require initialization? If so, this introduces even further complications involving data
statements or even the block data
construct.
这篇关于通用块和子例程参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!