Interface mismatch - higher order functions

前端 未结 1 1807
花落未央
花落未央 2021-01-16 20:19

I\'m trying to \'reproduce\' higher order functions in fortran.

module rk4

contains



 pure function f(t,x) result (fx)
    real, dimension(1), intent(in)         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 20:45

    The error message is complaining about the function f being incompatible with the dummy argument f.

    You declare it as

       real, external :: f
    

    which means it should return a scalar whereas in reality the function f returns an array.

    The same names do not really help the understanding here. I changed the name of the dummy argument in the following code to g.

    The easiest way to solve this is

     pure function f(t,x) result (fx)
        real, dimension(1), intent(in) :: x
        real, intent(in) :: t
        real, dimension(1) ::  fx
    
        fx = x
      end function f
    
      function step(x,g,dt) result(xn)
        real, intent(in) :: dt
        real, intent(in),  dimension(:) :: x
        real, dimension(:), allocatable :: xn
        procedure(f) :: g
    
        !here call g, not f!!!
    

    The procedure statement comes from Fortran 2003 and causes the dummy argument procedure g to have the same interface as the procedure f.

    Otherwise you can use an interface block:

      function step(x,g,dt) result(xn)
        real, intent(in) :: dt
        real, intent(in),  dimension(:) :: x
        real, dimension(:), allocatable :: xn
    
        interface
          pure function g(t,x) result (fx)
            real, dimension(1), intent(in) :: x
            real, intent(in) :: t
            real, dimension(1) ::  fx
          end function g
        end interface
    

    The external statement should be used in modern code only in some exceptional cases.

    0 讨论(0)
提交回复
热议问题