Explicit interfaces
Why interfaces matter
An interface tells the compiler the argument count, types, and order of a procedure: with it every call is checked, and without it mistakes slip to runtime.
Count and type errors are the most common procedure bugs, and an explicit interface turns them into quick compile errors.
program whyface
use stats
implicit none
print *, double_it(21)
end program whyface
gfortran stats.f90 main.f90 -o whyface && ./whyface
42
Modules give them free
Every module procedure carries an explicit interface automatically: use the module and the compiler already knows each signature.
This is the main reason modern Fortran lives in modules: safety with zero extra syntax.
module mtools
implicit none
contains
integer function triple(x)
implicit none
integer, intent(in) :: x
triple = x * 3
end function triple
end module mtools
program modfree
use mtools
implicit none
print *, triple(14)
end program modfree
gfortran mtools.f90 main.f90 -o modfree && ./modfree
42
Interface blocks
For external procedures with no module, an interface block states the signature by hand inside the caller.
Copy the specification part exactly: the name, the dummy types, and the intents must mirror the real procedure.
program ibdemo
implicit none
interface
integer function cube(x)
implicit none
integer, intent(in) :: x
end function cube
end interface
print *, cube(3)
end program ibdemo
integer function cube(x)
implicit none
integer, intent(in) :: x
cube = x ** 3
end function cube
gfortran main.f90 -o ibdemo && ./ibdemo
27
Catch mismatches
A wrong type argument now fails at compile time: the interface gives the compiler the facts it needs to reject the call.
Fix the call or the interface, and the program never starts with corrupt arguments.
program badcall
implicit none
interface
integer function cube(x)
implicit none
integer, intent(in) :: x
end function cube
end interface
print *, cube("oops")
end program badcall
gfortran bad.f90 -o bad
Error: Type mismatch in argument 'x' at (1); passed CHARACTER(4) to INTEGER(4)
The code on this page was run with gfortran 15.2.0 on Alpine Linux 3.24 (x86_64) in a Docker container.
Article author: Arthur Isaev