Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 39 ยท Procedures and Modules

Interfaces, Generic Procedures and Operator Overloading

Use interface blocks to write generic procedures that accept several types, overload operators, pass procedures as arguments, and describe external code.

An interface describes how to call a procedure: its name, the types and attributes of its arguments, and its result. Procedures in modules get interfaces automatically. Explicit interface blocks do three other jobs: they let several procedures share one generic name, they let you overload operators for your own types, and they describe procedures the compiler cannot see, including procedure arguments.

Generic procedures

The intrinsic abs works on integers, reals and complex numbers. You can build the same convenience:

FORTRAN
module swap_mod
  implicit none
  private
  public :: swap

  interface swap
    module procedure swap_int, swap_real, swap_str
  end interface swap

contains

  subroutine swap_int(a, b)
    integer, intent(inout) :: a, b
    integer :: t
    t = a; a = b; b = t
  end subroutine swap_int

  subroutine swap_real(a, b)
    real, intent(inout) :: a, b
    real :: t
    t = a; a = b; b = t
  end subroutine swap_real

  subroutine swap_str(a, b)
    character(*), intent(inout) :: a, b
    character(len(a)) :: t
    t = a; a = b; b = t
  end subroutine swap_str

end module swap_mod

Now call swap(i, j) with integers, call swap(x, y) with reals and call swap(s1, s2) with strings all compile, and the compiler picks the matching specific procedure by argument types, kinds and ranks. The specific names stay private, so users see only swap.

The rule for a legal generic: any two specifics must be distinguishable by their non-optional arguments, so the choice is unambiguous. Two procedures whose only difference is a result type or an intent cannot share a name.

Python Stand-in: dispatching on type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Kinds and generics

A common use is supporting two precisions with one name, for example real(sp) and real(dp):

FORTRAN
interface mean
  module procedure mean_sp, mean_dp
end interface mean

Users write mean(a) and never think about precision. The standard library's own sqrt, sin and sum are built this way.

Overloading operators

You can give +, -, *, == and the rest a meaning for derived types with interface operator(...):

FORTRAN
module vec_mod
  implicit none
  type :: vec2
    real :: x, y
  end type vec2

  interface operator(+)
    module procedure vec_add
  end interface

  interface operator(.dot.)
    module procedure vec_dot
  end interface

contains

  pure function vec_add(a, b) result(c)
    type(vec2), intent(in) :: a, b
    type(vec2) :: c
    c = vec2(a%x + b%x, a%y + b%y)
  end function vec_add

  pure function vec_dot(a, b) result(d)
    type(vec2), intent(in) :: a, b
    real :: d
    d = a%x * b%x + a%y * b%y
  end function vec_dot

end module vec_mod

With use vec_mod you may write p + q and p .dot. q. Operator functions must be pure-like: arguments intent(in), a result, and one (unary) or two (binary) arguments. You can also invent defined operators with dots, as .dot. above. For assignment, interface assignment(=) takes a subroutine whose first argument is intent(out) or intent(inout) and second is intent(in).

Python Stand-in: operator overloading

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

flowchart LR
    A["call swap(i, j)"] --> B{"generic swap"}
    B -->|integer, integer| C["swap_int"]
    B -->|real, real| D["swap_real"]
    B -->|character, character| E["swap_str"]

The compiler chooses the specific procedure at compile time from the argument types.

Procedure arguments

To pass a procedure to another procedure (a function to integrate, a callback), describe it with an interface block. It is called an abstract interface when no real procedure carries the name:

FORTRAN
module integrate_mod
  implicit none

  abstract interface
    function fn(x) result(y)
      real, intent(in) :: x
      real :: y
    end function fn
  end interface

contains

  function trapezoid(f, a, b, n) result(s)
    procedure(fn) :: f
    real, intent(in) :: a, b
    integer, intent(in) :: n
    real :: s, h
    integer :: i
    h = (b - a) / n
    s = 0.5 * (f(a) + f(b))
    do i = 1, n - 1
      s = s + f(a + i * h)
    end do
    s = s * h
  end function trapezoid

end module integrate_mod

procedure(fn) :: f declares a dummy procedure with the same interface as fn. A caller passes any function whose arguments and result match: trapezoid(my_square, 0.0, 3.0, 1000). The integral of x squared from 0 to 3 is 9, and the trapezoid rule gets very close.

Python Stand-in: trapezoid rule with a function argument

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Interface blocks for external code

For an old external procedure or a library routine written in another language, an interface block supplies what a module would have supplied:

FORTRAN
interface
  subroutine legacy_solve(a, n, info)
    integer, intent(in) :: n
    real, intent(inout) :: a(n, n)
    integer, intent(out) :: info
  end subroutine legacy_solve
end interface

If your block disagrees with the real procedure, the compiler cannot know. Treat interface blocks for external code as a contract you must keep in sync.

Common mistakes

  • Two specifics whose arguments differ only by name or by intent: ambiguous generic.
  • Listing a procedure in module procedure that does not exist in the same module.
  • Forgetting to make specific names private, which clutters the user's namespace.
  • Defining operator(+) with an argument that is not intent(in).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a generic interface give you?

    The compiler resolves the call to a specific procedure from the actual argument types, kinds and ranks.
  2. 2. How do you give + a meaning for a derived type?

    An operator interface lists a function that takes the operands as intent(in) arguments and returns the result.
  3. 3. What is procedure(fn) :: f used for?

    It states that f is a procedure argument whose interface matches fn, so a caller may pass any compatible function.
  4. 4. Which pair can NOT share a generic name?

    Specifics must be distinguishable by their argument types, kinds, ranks or counts, not by names alone.

Summary

  • Module procedures have explicit interfaces automatically; interface blocks add generics and describe external or abstract procedures.
  • interface name + module procedure builds a generic name resolved at compile time.
  • interface operator(...) and interface assignment(=) overload operators.
  • abstract interface plus procedure(name) :: f passes functions as arguments.

Next: Recursion and pure procedures