Take the 2-minute tour ×

I want to create a subroutine that gives back a function as an output, How can I do that? I'll put an example of how I think it should be(I know it's bad written)

module fun_out


subroutrine exponential(F,a)
     interface, intent(out)

        function f(x)
         real, intent(in)::x
         real :: f(2)
        end function
     end interface
     real,intent(in):: a


end subroutine exponential

end module

[with this i should take a function from the exponentials family in the output]

Thank you, and sorry for my bad English (and for the writing thing, the code didn't entered in the grey zone and i don't know why)

share|improve this question
add comment

2 Answers

You can basically do that (as also mentioned in Vladimir's answer) with defining functor objects. They have one specific function returning the value (e.g. getvalue()), and depending on their initialization, they may return customized function values.

The example below demonstrates that in detail. The general functor is defined in functor_module, in expfunc_module a concrete realization for the exponential function family is derived. Then, in the main program then you initialize different instances with different prefactors in the exponents and can use their getvalue() method to obtain the appropriate function values.:

module functor_module
  implicit none

  integer, parameter :: wp = kind(1.0d0)

  type, abstract :: functor
    procedure(getvalue_iface), deferred :: getvalue
  end type functor

    function getvalue_iface(self, xx) result(yy)
      class(functor), intent(in) :: self
      real(wp), intent(in) :: xx
      real(wp) :: yy
    end function getvalue_iface
  end interface

end module functor_module

module expfunc_module
  use functor_module
  implicit none

  type, extends(functor) :: expfunc
    real(wp) :: aa
    procedure :: getvalue
  end type expfunc


  function getvalue(self, xx) result(yy)
    class(expfunc), intent(in) :: self
    real(wp), intent(in) :: xx
    real(wp) :: yy

    yy = exp(self%aa * xx)

  end function getvalue

end module expfunc_module

program test_functors
  use expfunc_module
  implicit none

  type(expfunc) :: func1, func2
  real(wp) :: xx

  func1 = expfunc(1.0_wp)
  func2 = expfunc(2.0_wp)
  xx = 1.0_wp
  print *, func1%getvalue(xx)   ! gives exp(1.0 * xx) = 2.718...
  print *, func2%getvalue(xx)   ! gives exp(2.0 * xx) = 7.389...

end program test_functors
share|improve this answer
add comment

You would have to return a function pointer. That can be done in Fortran 2003.

   procedure(name_of_interface), pointer :: f

You must however not expect full lexical scope closures, just pure pointers.

You must have the procedure prepared as a normal external, module or in F2008 even internal procedure (with some limitations) and just point to it:

    f => my_function

In your case you have the argument a, and you seem to want to use it a captured closure variable. It is not possible in Fortran. You either have to pass it every time to the function, or us the Functor pattern (derived type holding the captured parameters), or use an internal procedure (but that would be valid only inside its host procedure).

share|improve this answer
add comment

Your Answer


