fortran - 通过子程序将一组变量值传递给没有公共(public) block 的函数有哪些方法?

标签 fortran fortran90 fortran77

我不想在我的程序中使用公共(public) block 。我的主程序调用一个调用函数的子程序。该函数需要来自子例程的变量。

将信息集从子程序传递给函数的方式有哪些?

program
...

call CONDAT(i,j)

end program

SUBROUTINE CONDAT(i,j)

common /contact/ iab11,iab22,xx2,yy2,zz2
common /ellip/ b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2
call function f(x)
RETURN
END

function f(x)
common /contact/ iab11,iab22,xx2,yy2,zz2
common /ellip/ b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2
end

最佳答案

您在这里关心的是关联:您希望能够将函数f 中的实体与子例程condat 中的实体关联起来.存储关联是实现此目的的一种方式,这就是公共(public) block 正在做的事情。

还有其他有用的关联形式。这些是

  • 使用联想
  • 主办协会
  • 论证协会

haraldkl's answer 中描述了参数关联.

使用关联来自于像这样的模块

module global_variables
  implicit none     ! I'm guessing on declarations, but that's not important
  public   ! Which is the default
  real b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2,xx2,yy2,zz2
  integer iab11,iab22
end module

subroutine condat(i,j)
  use global_variables   ! Those public things are use associated
  ...
end subroutine

function f(x)
  use global_variables   ! And the same entities are accessible here
  ...
end function

主机关联可以访问主机可访问的实体。这里的宿主可以是一个模块或程序

module everything
  integer iab11,...
  real ...
 contains
  subroutine condat(i,j)
    ! iab11 available from the host module
  end subroutine

  function f(x)
    ! iab11 available from the host module
  end function
end module

甚至子程序本身

subroutine condat(i,j)
  integer iab11,...
  real ...
 contains
  function f(x)
    ! Host condat's iab11 is accessible here
  end function
 end subroutine

关于fortran - 通过子程序将一组变量值传递给没有公共(public) block 的函数有哪些方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386146/

相关文章:

fortran - 为什么 COBOL 必须缩进?

fortran - Fortran 90 中的中点规则

fortran - BLAS 函数在 Fortran90 中返回零

c++ - 如何从 C++ 调用 fortran 例程?

fortran - 从 Fortran IV 更新到 Fortran 77

java - 如何使用 JNA 处理 Fortran RTL 错误?

fortran - 是否有 LU 分解的命令或子程序?

c++ - 将 C 字符串和 fortran 字符串混合在一个文件中

parsing - 将关键字后面的数字从文本文件读取到 Fortran 90 中的数组中

python - 将 Fortran 77 代码转换为 Python