1

I am trying to pass a function as an argument to another function. When I try using a simple function it works fine. But when I try to pass a function belonging to an object I get an error. Here's a simple code to recreate the problem in Fortran.

module classb

  use iso_c_binding

  implicit none

  type :: fr
      integer(c_int) :: order

      contains
          procedure, pass   :: get_val
          procedure, nopass :: fn

  end type fr

  contains

      subroutine get_val(this)
          class(fr), intent(inout)   :: this

          write(*, *) this%order

      end subroutine get_val

      function fn(a) result(b)
          integer(c_int), intent(in) :: a
          integer(c_int) :: b

          b = 2*a
      end function fn

 end module classb

 module callb

  use iso_c_binding
  use classb

  implicit none

  interface
      function func(a) result(b)
          use iso_c_binding
          implicit none
          integer(c_int), intent(in) :: a
          integer(c_int) :: b
      end function func
  end interface

  contains

      subroutine do_call_b(f, a)
          procedure(fn) :: f
          integer(c_int) :: a

          write(*, *) f(a)

      end subroutine do_call_b

      subroutine get_value()
          type(fr) :: f

          f = fr(order = 1)

          call f%get_val()

          call do_call_b(f%fn, 1)

      end subroutine get_value

end module callb

program main

  use callb

  call get_value()

end program main

I get the error

          call do_call_b(f%fn, 1)
                            1
 Error: Expected argument list at (1)

Is callbacks using objects not supported in Fortran. If so how could I do something like this.

Vikram
  • 308
  • 1
  • 5
  • subroutines get called, and functions return something. You are calling a function, so maybe get_val needs to be a function too? – Holmz Dec 21 '16 at 16:31
  • `f%fn` isn't a function, it's a binding. See [this question](https://stackoverflow.com/q/11318816), for example. – francescalus Dec 21 '16 at 16:41
  • Sorry for that. Needed to know the correct terms to search. – Vikram Dec 22 '16 at 10:12

0 Answers0