使用fortran子例程参数列表中的变量来设置相同名称的模块变量 [英] Use variable from fortran subroutine argument list to set module variable of the same name

查看:353
本文介绍了使用fortran子例程参数列表中的变量来设置相同名称的模块变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个称为Fortran的模块中定义了一堆变量.我想要一个子例程,该子例程最简单的方法是将模块级别的变量初始化为用户提供的变量.下面的简化代码可以工作,但是,我不得不在子例程参数列表中使用虚拟变量"iii_"来最终设置模块变量"iii"的值.有什么方法可以在子例程参数列表和模块中使用相同的变量名?

I have a whole bunch of variables defined in a Fortran module called. I want to have a subroutine that, at its simplest, initializes the module level variables to those provided by the user. The simplified code below works, however, I've had to use a dummy variable "iii_" in the subroutine argument list to ultimately set the value of module variable "iii". Is there any way to use the same variable name in the subroutine argument list and the module?

  MODULE foo        
    IMPLICIT NONE
    INTEGER :: iii

    CONTAINS
      SUBROUTINE initilize(iii_)
        IMPLICIT NONE
        INTEGER :: iii_
        iii = iii_
        print *, iii
      END SUBROUTINE
  END MODULE

所以我真正想要的是:

  MODULE foo        
    IMPLICIT NONE
    INTEGER :: iii

    CONTAINS
      SUBROUTINE initilize(iii)
        IMPLICIT NONE
        [code to set subroutine iii to module iii]
        print *, iii
      END SUBROUTINE
  END MODULE

推荐答案

否.您不能在相同的作用域中具有相同名称的模块变量和哑元参数,后者将具有优先权,并且您将无法在子例程中访问模块变量.

No. You cannot have a module variable and a dummy argument with the same name in the same scope, the later would take precedence and you would have no access to the module variable in the subroutine.

但是,您可以使用第二个模块进行初始化,并使用其他名称的foo中的模块变量:

You could, however, have a second module just for initialization and use the module variables from foo with another name:

MODULE bar
  CONTAINS
    SUBROUTINE initilize(iii)
      USE foo, ONLY: iii_ => iii
      IMPLICIT NONE
      INTEGER :: iii

      iii_ = iii
    END SUBROUTINE
END MODULE

这篇关于使用fortran子例程参数列表中的变量来设置相同名称的模块变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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