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

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

问题描述

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



将子程序中的信息传递给函数的方法是什么?

 程序
...

调用CONDAT(i,j)

结束程序

子程序(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
调用函数f(x)
RETURN
END

函数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 中的实体与子例程<$ c $中的实体相关联C> CONDAT 。存储关联是实现这一点的一种方式,这就是常用的块。



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


  • 使用关联

  • 主机关联

  • 参数关联



参数关联在 haraldkl's answer



使用关联来自模块,如

 模块global_variables 
隐式无!我正在猜测声明,但这不重要
public!这是默认的
real b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2,xx2,yy2,zz2
integer iab11 ,iab22
end模块

子程序condat(i,j)
使用global_variables!这些公共事物是使用相关的
...
结束子程序

函数f(x)
使用global_variables!在这里可以访问相同的实体
...
结束函数

主机关联有权访问主机可访问的实体。这里的主机可能是一个有用的模块或程序

 模块所有
整数iab11,...
真实...
包含
子程序condat(i,j)
! iab11可从主机模块获得
结束子程序

函数f(x)
! iab11可从主机模块获得
结束功能
结束模块

甚至是子程序本身

 子程序condat(i,j)
整数iab11,...
real .. 。
包含
函数f(x)
!主机condat的iab11可在此访问
结束功能
结束子程序


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

解决方案

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

  • use association
  • host association
  • argument association

Argument association is described in haraldkl's answer.

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

or even the subroutine itself

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天全站免登陆