Fortran Wiki
interface_mod

A simple Fortran 2003 example illustrating how one can define common procedure interfaces in a module and reuse them elsewhere.

! A module containing function interfaces
module interfaces
  implicit none

  abstract interface
     real function func(x)
       real :: x
     end function func
  end interface

end module interfaces

! A module containing a function which implements func
module functions
  implicit none

contains

  real function square(x)
    real :: x
    square = x*x
  end function square

end module functions

! A simple control program showing how to use the func interface
program main
  use interfaces
  use functions
  implicit none

  call print_func(square)

contains

  subroutine print_func(f)
    procedure(func) :: f
    print *, f(0.0)
  end subroutine print_func

end program main

Jason Blevins (7 May 2009) I wrote this example just to test the concept. I think the abstract interface and the procedure statement in print_func are the only places where the Fortran 2003 standard is needed. Is there any way to accomplish the same thing in Fortran 95 without duplicating the interface (and without using include)? I suppose the answer is probably no, and this is the reason that abstract interfaces were defined in Fortran 2003.

category: code