我不想在我的程序中使用公共(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/