How to store a set of functions into a Fortran array

后端 未结 1 617
攒了一身酷
攒了一身酷 2021-01-17 03:02

As we know that function name can be treated as parameters to pass in/out by other subroutines. I wonder if we have any tricks to save a list of functions into an array, whi

相关标签:
1条回答
  • 2021-01-17 03:26

    Firstly you should create a type that contain only a procedure pointer that doesn't pass any argument. and then create an array of that type.

    Example:

    program test_func_array
       implicit none
    
       type pp
        procedure(func) ,pointer ,nopass :: f =>null()
       end type pp
    
       interface
          function func(x)
             real :: func
             real, intent (in) :: x
          end function func
       end interface
    
       type(pp) :: func_array(4)
    
       func_array(1)%f => exp
       func_array(2)%f => tan
       func_array(3)%f => cos
       func_array(4)%f => sin
    
       print*,func_array(1)%f(1.)
       print*,func_array(2)%f(1.)
       print*,func_array(3)%f(0.)
       print*,func_array(4)%f(0.)  
    
    end program test_func_array
    
    0 讨论(0)
提交回复
热议问题