What are the ways to pass a set of variable values through the subroutine to a function without common block?

后端 未结 3 650
孤独总比滥情好
孤独总比滥情好 2020-12-04 03:09

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 a

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 03:47

    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
    

提交回复
热议问题