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

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

    Below is an example of how you may achieve this...

    The code has been adapted from a BFGS method to show how you can pass functions and call other functions within a module...

    Here I use:

    • private functions nested within other subroutines
    • pass variables from a subroutine to a nested function
    • pass a function as an argument for a function that can be defined outside the module block

    Hopefully this will cover everything for you...

    Module Mod_Example
    
    Private :: private_func
    
       SUBROUTINE test_routine(res,start,fin,vector,func,dfunc)
          IMPLICIT NONE
          REAL, DIMENSION(:), INTENT(IN) :: res, start, fin
          REAL, DIMENSION(:), INTENT(INOUT) :: vector
    
          INTERFACE
             FUNCTION func(vector)                                      
                IMPLICIT NONE                                      
                REAL, DIMENSION(:), INTENT(IN) :: vector                
                REAL :: func                                       
             END FUNCTION func                                     
    
             FUNCTION dfunc(vector)                                     
                IMPLICIT NONE                                      
                REAL, DIMENSION(:), INTENT(IN) :: vector               
                REAL, DIMENSION(size(vector)) :: dfunc                  
             END FUNCTION dfunc                                    
          END INTERFACE
    
          ! do stuff with p
    
          private_func(res,start,fin,vector,func,dfunc) 
    
          ! do stuff
       END SUBROUTINE test_routine
    
       SUBROUTINE private_func(res,start,fin,vector,func,dfunc)
          IMPLICIT NONE
          REAL, DIMENSION(:), INTENT(IN) :: res, start, fin
          REAL, DIMENSION(:), INTENT(INOUT) :: vector
          INTERFACE
             FUNCTION func(vector)            
                REAL, DIMENSION(:), INTENT(IN) :: vector
                REAL :: func
             END FUNCTION func
             FUNCTION dfunc(vector)
                REAL, DIMENSION(:), INTENT(IN) :: vector
                REAL, DIMENSION(size(vector)) :: dfunc
             END FUNCTION dfunc     
          END INTERFACE   
    
          ! do stuff             
       END SUBROUTINE private_func
    
    END Mod_Example
    
    • func and dfunc would be declared within the program code that uses the MODULE Mod_Example with an interface block at the top.
    • the variables: res, start etc. can be declared with values in the main program block and passed to SUBROUTINE test_routine as arguments.
    • SUBROUTINE test_routine will call private_func with the variables that were passed to it.

    Your main program would then look something like this:

    Program Main_Program
       USE Mod_Example
       INTERFACE
          FUNCTION func(vector)            
             REAL, DIMENSION(:), INTENT(IN) :: vector
             REAL :: func
          END FUNCTION func
          FUNCTION dfunc(vector)
             REAL, DIMENSION(:), INTENT(IN) :: vector
             REAL, DIMENSION(size(vector)) :: dfunc
          END FUNCTION dfunc     
       END INTERFACE
    
       ! do stuff       
    
       ! calls test_routine form module
       ! uses dfunc and func defined below
       call test_routine(res,start,fin,vector,func,dfunc)
    
       ! do stuff
    END PROGRAM Main_Program
    
    ! define dfunc and nfunc for passing into the modular subroutine
    FUNCTION func(vector)
       IMPLICIT NONE
       REAL, DIMENSION(:), INTENT(IN) :: vector
       REAL :: func
    
       nfunc = vector
    END FUNCTION func
    
    FUNCTION dfunc(vector)
       IMPLICIT NONE
       REAL, DIMENSION(:), INTENT(IN) :: vector
       REAL, DIMENSION(size(vector)) :: dfunc   
    
       dfunc = vector
    END FUNCTION dfunc
    

提交回复
热议问题