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

后端 未结 3 647
孤独总比滥情好
孤独总比滥情好 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:39

    So, basically you could solve this with something along these lines:

    SUBROUTINE CONDACT(i,j, iab11,iab22,xx2,yy2,zz2,b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2,res)
      !declaration to all those parameters and res
      res = f(x)
    END SUBROUTINE CONDACT
    
    function f(x,iab11,iab22,xx2,yy2,zz2,b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2)
    !declaration to all those parameters
    end function f
    
    program
      ...
    
      call CONDAT(i,j,iab11,iab22,xx2,yy2,zz2,b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2,res)
    
    end program
    

    That is, just passing the parameters through. It is strongly encouraged to use modules, see Alexander McFarlane's answer, though it is not required. Alexander McFarlane shows how to pass f as an argument to the subroutine, such that you could use different functions in the subroutine, but your code does not seem to require this.

    Now, this is an awful long list of parameters, and you probably do not want to carry those around all the time. The usual approach to deal with this, is to put those parameters into a derived datatype and then just passing this around. Like this:

    !> A module implementing ellip related stuff.
    module ellip_module
    
      implicit none
    
      type ellip_type
        !whatever datatypes these need to be...
        integer :: b1,c1,f1,g1,h1,d1,b2,c2,f2,g2,h2,p2,q2,r2,d2
      end type
    end module ellip_module
    
    
    !> A module implementing condact related stuff.
    module condact_module
      use ellip_module ! Make use of the ellip module to have the type available
    
      implicit none
    
      type condact_type
        !whatever datatypes these need to be...
        integer :: iab11,iab22,xx2,yy2,zz2
      end type
    
      contains
    
      subroutine condact(i,j, con, ellip, res)
         integer :: i,j
         type(condact_type) :: con
         type(ellip_type) :: ellip
         real :: res
    
         real :: x
         res = f(x, con, ellip)
      end subroutine condact
    
      function f(x, con, ellip) result(res)
        real :: x
        real :: res
        type(condact_type) :: con
        type(ellip_type) :: ellip
    
        res = !whatever this should do
      end function f
    end module condact_module
    
    
    !> A program using the condact functionality.
    program test_condact
      use ellip_module
      use condact_module
    
      implicit none
    
      type(condact_type) :: mycon
      type(ellip_type) :: myellip
      integer :: i,j
      real :: res
    
      call condact(i,j, mycon, myellip, res)
    end program test_condact
    

    This is just a rough sketch, but I got the impression this is what you are looking for.

提交回复
热议问题