有哪些方法可以通过子程序将一组变量值传递给没有公共块的函数? [英] What are the ways to pass a set of variable values through the subroutine to a function without common block?

查看:9
本文介绍了有哪些方法可以通过子程序将一组变量值传递给没有公共块的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在我的程序中使用公共块.我的主程序调用一个调用函数的子程序.该函数需要来自子程序的变量.

I do not want to use common blocks in my program. My main program calls a subroutine which calls a function. The function needs variables from the subroutine.

将一组信息从子程序传递给函数的方法有哪些?

What are the ways to pass the set of information from the subroutine to the function?

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

推荐答案

这里你关心的是association:你希望能够在函数f 与子例程 condat 中的那些.存储关联是实现此目的的一种方式,这也是 common 块正在做的事情.

What you care about here is association: you want to be able to associate entities in the function f with those in the subroutine condat. Storage association is one way to do this, which is what the common block is doing.

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

There are other forms of association which can be useful. These are

  • 使用关联
  • 主机关联
  • 参数关联

haraldkl 的回答中描述了参数关联.

使用关联来自于类似的模块

Use association comes through modules like

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

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

Host association is having access to entities accessible to the host. A host here could usefully be a module or a program

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

这篇关于有哪些方法可以通过子程序将一组变量值传递给没有公共块的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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