有什么方法可以将一组变量值通过子程序传递给没有公共块的函数?

sam*_*sam 2 fortran fortran77 fortran90

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

有什么方法可以将子程序中的信息集传递给函数?

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
Run Code Online (Sandbox Code Playgroud)

fra*_*lus 6

你关心的是关联:你希望能够将函数中的实体f与子例程中的实体相关联condat.存储关联是执行此操作的一种方式,这是公共块正在执行的操作.

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

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

haraldkl的答案描述了论证关联.

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

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
Run Code Online (Sandbox Code Playgroud)

主机关联可以访问主机可访问的实体.这里的主机可以有用地是模块或程序

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
Run Code Online (Sandbox Code Playgroud)

甚至子程序本身

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