如果我有一个名为var的变量,它位于一个名为myCB的公共块中,那么我可以使用相同的名称在另外两个不使用公共块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 SR4Var在SR1和SR2之间的传递确实有问题,这个问题是否来自公共块中的另一个名为Var的Var?
发布于 2016-12-02 17:19:58
如果您不想过多地修改遗留代码库,我建议您将common块放在一个module中,并在需要访问时导入变量:
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中。
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构造的复杂问题。
https://stackoverflow.com/questions/40936915
复制相似问题