Fortran Best Practices

Some things in Fortran are obsolete, so this guide only shows the "one correct/canonical modern way" how to do things.

Language features are explained at: http://en.wikipedia.org/wiki/Fortran_language_features

The best resource is a recent Fortran standard, for example the Fortran 2003 standard: http://www.j3-fortran.org/doc/year/04/04-007.pdf

Fortran Style Guide

Use the right instruction whenever you can (select case, where, do concurrent)

Naming Convention

Ultimately this is a matter of preference. Here is a style guide that we like and that seems to be prevalent in most scientific codes (as well as the Fortran standard library) and you are welcome to follow it.

  1. Use lowercase for all Fortran constructs (do, subroutine, module, …).
  2. Follow short mathematical notation for mathematical variables/functions (Ylm, Gamma, gamma, Enl, Rnl, …).
  3. For other names use all lowercase: try to keep names to one or two syllables; if more are required, use underscores to clarify (sortpair, whitechar, meshexp, numstrings, linspace, meshgrid, argsort, spline, spline_interp, spline_interpolate, stoperr, stop_error, meshexp_der).

For example "spline interpolation" can be shortened to spline_interpolation, spline_interpolate, spline_interp, spline, but not to splineint ("int" could mean integration, integer, etc. — too much ambiguity, even in the clear context of a computational code). This is in contrast to get_argument() where getarg() is perfectly clean and clear.

The above are general guidelines. In general, choosing the right name certainly depends on the word being truncated as to whether the first syllable is sufficient. Usually it is but clearly not always. Thus some thought should go into step "try to keep names to 2 syllables or less" since it can really affect the indicativeness and simplicity. Simple consistent naming rules are a real help in this regard – for both collaboration and for one's own sanity when going back to some old code you haven't seen in while.

Indentation

Use only spaces (not tabs) to indent the code.

For normal statement use min. 3 spaces.

Indent also after program, subroutine, function, contains and module instructions, for this statements you could use only 2 spaces to indent.

contains statement is not to be indented

program prova
  do i=1,25
     a = i * i
  end do

  if (ll) then
     return
  end if
end program prova

Numbers

Somewhere create and export parameters defining all kinds used in the whole program, in the most portable way:

! SELECTED_INT_KIND(R) return the kind value of the smallest integer type that 
! can represent all values ranging from -10^R (exclusive) to 10^R (exclusive). 
! If there is no integer kind that accommodates this range, SELECTED_INT_KIND 
! returns -1
integer, parameter :: ki1=selected_int_kind(1)
integer, parameter :: ki2=selected_int_kind(3)
integer, parameter :: ki4=selected_int_kind(8)
integer, parameter :: ki8=selected_int_kind(10)
! SELECTED_REAL_KIND(P,R) returns the kind value of a real data type with
! decimal precision of at least P digits, exponent range of at least R, 
! and with a radix of RADIX. 
integer, parameter :: ksp = selected_real_kind(6, 37)
integer, parameter :: kdp = selected_real_kind(15, 307)
integer, parameter :: kqp = selected_real_kind(33, 4931)
! SELECTED_REAL_KIND(P,R) returns the kind value of a real data type with
! decimal precision of at least P digits, exponent range of at least R, 
! and with a radix of RADIX. 
integer, parameter ::                           &
ksp = kind(1.0),                                &
kdp = selected_real_kind(2*precision(1.0_ksp)), &
kqp = selected_real_kind(2*precision(1.0_kdp))
use iso_fortran_env, only: INT8, INT16, INT32, INT64, REAL32, REAL64
...
integer, parameter :: ki1 = INT8
integer, parameter :: ki2 = INT16
integer, parameter :: ki4 = INT32
integer, parameter :: ki8 = INT64
integer, parameter :: ksp = REAL32
integer, parameter :: kdp = REAL64

and declare variables as:

integer(ki4) :: mm
integer(kind=ki4) :: nn
real(kdp)      :: a, 
real(kind=kdp) :: b, c

Always write all floating point constants with the kdp suffix:

1.0_kdp, 3.5_kdp, 1.34e8_kdp

and never any other way (see also the floating point gotcha). Omitting the dot in the literal constant is also incorrect.

To print floating point double precision numbers without losing precision, use the (es23.16) format (see why-do-i-need-17-significant-digits-and-not-16-to-represent-a-double).

It is safe to assign integers to floating point numbers without losing accuracy:

real(kdp) :: a
a = 3

In order to impose floating point division (as opposed to integer division 1/2 equal to 0), one can convert the integer to a floating point number by:

real(kdp) :: a, b
a = real(1, kdp) / 2       ! 'a' is equal to 0.5_kdp
! but also a = real(1, kind=kdp) / 2 
b = 1.0_kdp /2

Integer Division

when doing things like (N-1)/N where N is an integer and you want a floating point division, force the compiler to use floats at the right hand side, for example by:

(N - 1.0_kdp)/N

As long as one of the / operands is a float, a floating point division will be used.

Modules and Programs

Only use modules and programs. Always setup a module in the following way:

module ode1d
  use types, only: kdp, pi
  use utils, only: stop_error
  implicit none
  private
  public integrate, normalize, parsefunction, get_val, rk4step, eulerstep, &
      rk4step2, get_midpoints, rk4_integrate, rk4_integrate_inward, &
      rk4_integrate_inward2, rk4_integrate3, rk4_integrate4, &
      rk4_integrate_inward4

contains

  subroutine get_val(...)
  ...
  end subroutine get_val
  ...
end module ode1d

The implicit none statement works for the whole module (so you don't need to worry about it). By keeping the private empty, all your subroutines/data types will be private to the module by default. Then you export things by putting it into the public clause.

Setup programs in the following way:

program uranium
  use fmesh, only: mesh_exp
  use utils, only: stop_error, kdp
  use dft, only: atom
  implicit none

  integer, parameter :: Z = 92
  real(kind=kdp), parameter :: r_min = 8e-9_kdp, r_max = 50.0_kdp, a = 1e7_kdp
  ...
  print *, "I am running"
end program uranium

Notice the "explicit imports" (using Python terminology) in the use statements. You can also use "implicit imports" like:

use fmesh

"explicit imports" give you a better control of the namespace of your program, you should prefer it.

Repeat the name of the routine or program in the end instruction.

Use one file per module.

Procedures

Subroutine and function should have only one exit point

Write elemental function and subroutine whenever you can

Write pure function and subroutine whenever you can

Arrays

When passing arrays in and out of a subroutine/function, use the following pattern for arrays (it is called assumed-shape):

subroutine f(A, r)
  real(kdp), intent(out), dimension(:,:) :: A
  real(kdp), intent(out), dimension(:) :: r
  integer :: n, i
  n = size(r)
  do i = 1, n
     r(i) = 1.0_dp / i**2
  end do
  ! Some code to assign A
end subroutine f

and call it like this:

real(dp), dimension(5) :: r
call f(r)

No array copying is done above. It has the following advantages:

This should always be your default way of passing arrays in and out of subroutines. However in the following cases one can (or has to) use explicit-shape arrays:

To use explicit-shape arrays, do:

subroutine f(m, n, r, A)
  integer, intent(in) :: m, n
  real(kdp), intent(out), dimension(n) :: r ! 1D array
  real(kdp), intent(out), dimension(m,n) :: A ! 2D array
  integer :: i
  do i = 1, n
     r(i) = 1.0_kdp / i**2
  end do
  ! Some code to assign A
end subroutine f

and call it like this:

real(kdp) :: r(5)
call f(size(r), r)

In order to return an array from a function, do:

function f(n) result(r)
  integer, intent(in) :: n
  real(kdp), dimension(n) :: r
  integer :: i
  do i = 1, n
     r(i) = 1.0_kdp / i**2
  end do
end function f

If you want to enforce/check the size of the arrays, put at the beginning of the function:

if (size(r) /= 4) stop "Incorrect size of 'r'"

To initialize an array, do:

integer :: r(5)
r = [1, 2, 3, 4, 5]

This syntax is valid since the Fortran 2003 standard and it is the preferred syntax (the old syntax r = (/ 1, 2, 3, 4, 5 /) should only be used if you cannot use Fortran 2003).

In order for the array to start with different index than 1, do:

subroutine print_eigenvalues(kappa_min, lam)
  integer(ki4), intent(in) :: kappa_min
  real(kdp), intent(in), dimension(kappa_min:) :: lam

  integer(ki4) :: kappa
  do kappa = kappa_min, ubound(lam, 1)
     print *, kappa, lam(kappa)
  end do
end subroutine print_eigenvalues

Multidimensional Arrays

Always access slices as V(:, 1), V(:, 2), or V(:, :, 1), e.g. the colons should be on the left. That way the stride is contiguous and it will be fast. So when you need some slice in your algorithm, always setup the array in a way, so that you call it as above. If you put the colon on the right, it will be slow.

Example:

dydx = matmul(C(:, :, i), y) ! fast
dydx = matmul(C(i, :, :), y) ! slow

In other words, the "fortran storage order" is: smallest/fastest changing/innermost-loop index first, largest/slowest/outermost-loop index last ("Inner-most are left-most."). So the elements of a 3D array A(N1,N2,N3) are stored, and thus most efficiently accessed, as:

do i3 = 1, N3
   do i2 = 1, N2
      do i1 = 1, N1
         A(i1, i2, i3)
      end do
   end do
end do

Associated array of vectors would then be most efficiently accessed as:

do i3 = 1, N3
   do i2 = 1, N2
      A(:, i2, i3)
   end do
end do

And associated set of matrices would be most efficiently accessed as:

do i3 = 1, N3
   A(:, :, i3)
end do

Storing/accessing as above then accesses always contiguous blocks of memory, directly adjacent to one another; no skips/strides.

When not sure, always rewrite (in your head) the algorithm to use strides, for example the first loop would become:

do i3 = 1, N3
   Ai3 = A(:, :, i3)
   do i2 = 1, N2
      Ai2i3 = Ai3(:, i2)
      do i1 = 1, N1
         Ai2i3(i1)
      end do
   end do
end do

the second loop would become:

do i3 = 1, N3
   Ai3 = A(:, :, i3)
   do i2 = 1, N2
      Ai3(:, i2)
   end do
end do

And then make sure that all the strides are always on the left. Then it will be fast.

Element-wise Operations on Arrays Using Subroutines/Functions

There are three approaches:

In the first approach, one uses the elemental keyword to create a function like this:

elemental function nroot(n, x) result(y)
  real(kdp), intent(out) :: y
  integer, intent(in) :: n
  real(dp), intent(in) :: x
  y = x**(1._kdp / n)
end function nroot

All arguments (in and out) must be scalars. You can then use this function with arrays of any (compatible) shape, for example:

print *, nroot(2, 9._kdp)
print *, nroot(2, [1._kdp, 4._kdp, 9._kdp, 10._kdp])
print *, nroot(2, reshape([1._kdp, 4._kdp, 9._kdp, 10._kdp], [2, 2]))
print *, nroot([2, 3, 4, 5], [1._kdp, 4._kdp, 9._kdp, 10._kdp])
print *, nroot([2, 3, 4, 5], 4._kdp)

The output will be:

3.0000000000000000
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795
1.0000000000000000        1.5874010519681994        1.7320508075688772        1.5848931924611136
2.0000000000000000        1.5874010519681994        1.4142135623730951        1.3195079107728942

In the above, typically n is a parameter and x is the array of an arbitrary shape, but as you can see, Fortran does not care as long as the final operation makes sense (if one argument is an array, then the other arguments must be either arrays of the same shape or scalars). If it does not, you will get a compiler error.

The elemental keyword implies the pure keyword, so the subroutine must be pure (can only use pure subroutines and have no side effects).

If the elemental function's algorithm can be made faster using array operations inside, or if for some reason the arguments must be arrays of incompatible shapes, then one should use the other two approaches. One can make nroot operate on a vector and write a simple wrappers for other array shapes:

function nroot(n, x) result(y)
  integer, intent(in) :: n
  real(kdp), intent(in), dimension(:) :: x
  real(kdp), intent(out) :: y(size(x))
  y = x**(1._kdp / n)
end function nroot

function nroot_0d(n, x) result(y)
  integer, intent(in) :: n
  real(kdp), intent(in) :: x
  real(kdp), intent(out) :: y
  real(kdp) :: tmp(1)
  tmp = nroot(n, [x])
  y = tmp(1)
end function nroot_0d

function nroot_2d(n, x) result(y)
  integer, intent(in) :: n
  real(kdp), intent(in) :: x(:, :)
  real(kdp), intent(out) :: y(size(x, 1), size(x, 2))
  y = reshape(nroot(n, reshape(x, [size(x)])), [size(x, 1), size(x, 2)])
end function nroot_2d

And use as follows:

print *, nroot_0d(2, 9._dp)
print *, nroot(2, [1._dp, 4._dp, 9._dp, 10._dp])
print *, nroot_2d(2, reshape([1._dp, 4._dp, 9._dp, 10._dp], [2, 2]))

This will print:

3.0000000000000000
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795

Or one can use explicit-shape arrays as follows:

function nroot(n, k, x) result(y)
  integer, intent(in) :: n, k
  real(kdp), intent(in), dimension(k) :: x
  real(kdp), intent(out), dimension(k) :: y
  y = x**(1._kdp / n)
end function nroot

Use as follows:

print *, nroot(2, 1, [9._dp])
print *, nroot(2, 4, [1._dp, 4._dp, 9._dp, 10._dp])
print *, nroot(2, 4, reshape([1._dp, 4._dp, 9._dp, 10._dp], [2, 2]))

The output is the same as before:

3.0000000000000000
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795
1.0000000000000000        2.0000000000000000        3.0000000000000000        3.1622776601683795

Allocatable Arrays

When using allocatable arrays (as opposed to pointers), Fortran manages the memory automatically and it is not possible to create memory leaks.

For example you can allocate it inside a subroutine:

subroutine foo(lam)
  real(kdp), intent(out), dimension(:), allocatable :: lam
  allocate(lam(5))
end subroutine foo

And use somewhere else:

real(kdp), dimension(:), allocatable :: lam
call foo(lam)

When the lam symbol goes out of scope, Fortran will deallocate it. If allocate is called twice on the same array, Fortran will abort with a runtime error. One can check if lam is already allocated and deallocate it if needed (before another allocation):

if (allocated(lam)) deallocate(lam)
allocate(lam(10))

File Input/Output

To read from a file:

integer :: u
real(ksp) :: a, b
open(newunit=u, file="log.txt", status="old")
read(u, *) a, b
close(u)

Write to a file:

integer :: u
real(ksp) :: a, b
open(newunit=u, file="log.txt", status="replace")
write(u, *) a, b
close(u)

To append to an existing file:

integer :: u
open(newunit=u, file="log.txt", position="append", status="old")
write(u, *) N, V(N)
close(u)

The newunit keyword argument to open is a Fortran 2008 standard, in older compilers, just replace open(newunit=u, ...) by:

open(newunit(u), ...)

where the newunit function is defined by:

function newunit(unit) result(n)
! returns lowest i/o unit number not in use
  integer, intent(out) :: n
  integer, intent(out), optional :: unit
  logical :: inuse
  integer, parameter :: nmin=10   ! avoid lower numbers which are sometimes reserved
  integer, parameter :: nmax=999  ! may be system-dependent
  do n = nmin, nmax
     inquire(unit=n, opened=inuse)
     if (.not. inuse) then
        if (present(unit)) unit=n
        return
     end if
  end do
  call stop_error("newunit ERROR: available unit not found.")
end function newunit

Error handling

Subroutine and function must not stop the program, only pass signals to upper level procedure

Only the main program or a deputed infrastructure to the error management can stop the program and take the right decision

Type Casting in Callbacks

There are essentially five different ways to do that, each with its own advantages and disadvantages.

The methods I, II and V can be used both in C and Fortran. The methods III and IV are only available in Fortran. The method VI is obsolete and should not be used.

I: Work Arrays

Pass a "work array" or two which are packed with everything needed by the caller and unpacked by the called routine. This is the old way – e.g., how LAPACK does it.

Integrator:

module integrals
  use types, only: kdp
  implicit none
  private
  public simpson

contains

  function simpson(f, a, b, data) result(s)
    real(kdp) :: s
    real(kdp), intent(in) :: a, b
    interface
      function func(x, data)
        real(kdp) :: func
        use types, only: kdp
        implicit none
        real(kdp), intent(in) :: x
        real(kdp), intent(inout), dimension(:) :: data
      end function func
    end interface
    procedure(func) :: f
    real(kdp), dimension(:), intent(inout) :: data
    s = (b-a) / 6 * (f(a, data) + 4*f((a+b)/2, data) + f(b, data))
  end function simpson

end module

Usage:

 module test
   use types, only: kdp
   use integrals, only: simpson
   implicit none
   private
   public foo

 contains

  real(kdp) function f(x, data) result(y)
    real(kdp), intent(in) :: x
    real(kdp), intent(inout), dimension(:) :: data
    real(kdp) :: a, k
    a = data(1)
    k = data(2)
    y = a*sin(k*x)
  end function

  subroutine foo(a, k)
    real(kdp), intent(in) :: a, k
    real(kdp), dimension(2) :: data
    data(1) = a
    data(2) = k
    print *, simpson(f, 0._kdp, pi, data)
    print *, simpson(f, 0._kdp, 2*pi, data)
  end subroutine

end module

II: General Structure

Define general structure or two which encompass the variations you actually need (or are even remotely likely to need going forward). This single structure type or two can then change if needed as future needs/ideas permit but won't likely need to change from passing, say, real numbers to, say, and instantiation of a text editor.

Integrator:

module integrals
  use types, only: kdp
  implicit none
  private
  public simpson, context

  type context
  ! This would be adjusted according to the problem to be solved.
  ! For example:
    real(kdp) :: a, b, c, d
    integer :: i, j, k, l
    real(kdp), dimension(:), pointer :: x, y
    integer, dimension(:), pointer :: z
  end type context

contains

  function simpson(f, a, b, data) result(s)
    real(kdp), intent(out) :: s
    real(kdp), intent(in) :: a, b
    interface
      function func(x, data)
        real(kdp), intent(out) :: func 
        use types, only: kdp
        implicit none
        real(kdp), intent(in) :: x
        type(context), intent(inout) :: data
      end function func
    end interface
    procedure(func) :: f
    type(context), intent(inout) :: data
    s = (b-a) / 6 * (f(a, data) + 4*f((a+b)/2, data) + f(b, data))
  end function

end module

Usage:

module test
  use types, only: kdp
  use integrals, only: simpson, context
  implicit none
  private
  public foo

contains

  function f(x, data) result(y)
    real(dp), intent(out) :: y
    real(dp), intent(in) :: x
    type(context), intent(inout) :: data
    real(dp) :: a, k
    a = data%a
    k = data%b
    y = a*sin(k*x)
  end function

  subroutine foo(a, k)
    real(dp), intent(in) :: a, k
    type(context) :: data
    data%a = a
    data%b = k
    print *, simpson(f, 0._dp, pi, data)
    print *, simpson(f, 0._dp, 2*pi, data)
  end subroutine

end module

There is only so much flexibility really needed. For example, you could define two structure types for this purpose, one for Schroedinger and one for Dirac. Each would then be sufficiently general and contain all the needed pieces with all the right labels.

Point is: it needn't be "one abstract type to encompass all" or bust. There are natural and viable options between "all" and "none".

III: Private Module Variables

Hide the variable arguments completely by passing in module variables.

Integrator:

module integrals
  use types, only: kdp
  implicit none
  private
  public simpson

contains

function simpson(f, a, b) result(s)
  real(kdp), intent(out) :: s
  real(kdp), intent(in) :: a, b
  interface
    function func(x)
      use types, only: kdp
      implicit none
      real(kdp), intent(out) :: func
      real(kdp), intent(in) :: x
      end function
    end interface
    procedure(func) :: f
    s = (b-a) / 6 * (f(a) + 4*f((a+b)/2) + f(b))
  end function

end module

Usage:

module test
  use types, only: kdp
  use integrals, only: simpson
  implicit none
  private
  public foo

  real(kdp) :: global_a, global_k

contains

  function f(x) result(y)
    real(kdp), intent(out) :: y
    real(kdp), intent(in) :: x
    y = global_a*sin(global_k*x)
  end function

  subroutine foo(a, k)
    real(dp), intent(in) :: a, k
    global_a = a
    global_k = k
    print *, simpson(f, 0._dp, pi)
    print *, simpson(f, 0._dp, 2*pi)
  end subroutine

end module

However it is best to avoid such global variables – even though really just semi-global – if possible. But sometimes it may be the simplest cleanest way. However, with a bit of thought, usually there is a better, safer, more explicit way along the lines of II or IV.

IV: Nested functions

Integrator:

module integrals
  use types, only: dp
  implicit none
  private
  public simpson

contains

  real(dp) function simpson(f, a, b) result(s)
    real(dp), intent(in) :: a, b
    interface
      real(dp) function func(x)
      use types, only: dp
      implicit none
      real(dp), intent(in) :: x
      end function
    end interface
    procedure(func) :: f
    s = (b-a) / 6 * (f(a) + 4*f((a+b)/2) + f(b))
  end function

end module

Usage:

subroutine foo(a, k)
  use integrals, only: simpson
  real(dp) :: a, k
  print *, simpson(f, 0._dp, pi)
  print *, simpson(f, 0._dp, 2*pi)

contains

  real(dp) function f(x) result(y)
    real(dp), intent(in) :: x
    y = a*sin(k*x)
  end function f

end subroutine foo

V: Using type(c\ptr) Pointer

In C, one would use the void * pointer. In Fortran, one can use type(c_ptr) for exactly the same purpose.

Integrator:

module integrals
  use types, only: dp
  use iso_c_binding, only: c_ptr
  implicit none
  private
  public simpson

contains

  real(dp) function simpson(f, a, b, data) result(s)
    real(dp), intent(in) :: a, b
    interface
      real(dp) function func(x, data)
        use types, only: dp
        implicit none
        real(dp), intent(in) :: x
        type(c_ptr), intent(in) :: data
      end function
    end interface
    procedure(func) :: f
    type(c_ptr), intent(in) :: data
    s = (b-a) / 6 * (f(a, data) + 4*f((a+b)/2, data) + f(b, data))
  end function

end module

Usage:

module test
  use types, only: dp
  use integrals, only: simpson
  use iso_c_binding, only: c_ptr, c_loc, c_f_pointer
  implicit none
  private
  public foo

  type f_data
    ! Only contains data that we need for our particular callback.
    real(dp) :: a, k
  end type f_data

contains

  real(dp) function f(x, data) result(y)
    real(dp), intent(in) :: x
    type(c_ptr), intent(in) :: data
    type(f_data), pointer :: d
    call c_f_pointer(data, d)
    y = d%a * sin(d%k * x)
  end function

  subroutine foo(a, k)
    real(dp) :: a, k
    type(f_data), target :: data
    data%a = a
    data%k = k
    print *, simpson(f, 0._dp, pi, c_loc(data))
    print *, simpson(f, 0._dp, 2*pi, c_loc(data))
  end subroutine

end module

As always, with the advantages of such re-casting, as Fortran lets you do if you really want to, come also the disadvantages that fewer compile- and run-time checks are possible to catch errors; and with that, inevitably more leaky, bug-prone code. So one always has to balance the costs and benefits.

Usually, in the context of scientific programming, where the main thrust is to represent and solve precise mathematical formulations (as opposed to create a GUI with some untold number of buttons, drop-downs, and other interface elements), simplest, least bug-prone, and fastest is to use one of the previous approaches.

VI: transfer() Intrinsic Function

Before Fortran 2003, the only way to do type casting was using the transfer intrinsic function. It is functionally equivalent to the method V, but more verbose and more error prone. It is now obsolete and one should use the method V instead.

Examples:

VII: Object Oriented Approach

The module:

module integrals
  use types, only: dp
  implicit none
  private

  public :: integrand, simpson

  ! User extends this type
  type, abstract :: integrand
  contains
      procedure(func), deferred :: eval
  end type integrand

  abstract interface
    function func(this, x) result(fx)
      import :: integrand, dp
      class(integrand) :: this
      real(dp), intent(in) :: x
      real(dp) :: fx
    end function
  end interface

contains

  real(dp) function simpson(f, a, b) result(s)
    class(integrand) :: f
    real(dp), intent(in) :: a, b
    s = ((b-a)/6) * (f%eval(a) + 4*f%eval((a+b)/2) + f%eval(b))
  end function

end module

The abstract type prescribes exactly what the integration routine needs, namely a method to evaluate the function, but imposes nothing else on the user. The user extends this type, providing a concrete implementation of the eval type bound procedure and adding necessary context data as components of the extended type.

Usage:

module example_usage
  use types, only: dp
  use integrals, only: integrand, simpson
  implicit none
  private

  public :: foo

  type, extends(integrand) :: my_integrand
      real(dp) :: a, k
  contains
      procedure :: eval => f
  end type my_integrand

contains

  function f(this, x) result(fx)
    class(my_integrand) :: this
    real(dp), intent(in) :: x
    real(dp) :: fx
    fx = this%a*sin(this%k*x)
  end function

  subroutine foo(a, k)
    real(dp) :: a, k
    type(my_integrand) :: my_f
    my_f%a = a
    my_f%k = k
    print *, simpson(my_f, 0.0_dp, 1.0_dp)
    print *, simpson(my_f, 0.0_dp, 2.0_dp)
  end subroutine

end module

Complete Example of void * vs type(c\ptr) and transfer()

Here are three equivalent codes: one in C using void * and two codes in Fortran using type(c_ptr) and transfer():

Language Method Link
C void * https://gist.github.com/1665641
Fortran type(c_ptr) https://gist.github.com/1665626
Fortran transfer() https://gist.github.com/1665630

The C code uses the standard C approach for writing extensible libraries that accept callbacks and contexts. The two Fortran codes show how to do the same. The type(c_ptr) method is equivalent to the C version and that is the approach that should be used.

The transfer() method is here for completeness only (before Fortran 2003, it was the only way) and it is a little cumbersome, because the user needs to create auxiliary conversion functions for each of his types. As such, the type(c_ptr) method should be used instead.