1

如果我有一个名为的变量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 SR4

我在andVar之间传递确实有问题,问题可能来自公共块中命名的另一个问题吗?SR1SR2Var

4

1 回答 1

2

如果你不想过多修改遗留代码库,我建议你将common块放在 a 中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构造的更多复杂性。

于 2016-12-02T17:19:58.763 回答