Interfaces and explicit interfaces
A call is compiled from two things: the call itself and what the compiler knows about the procedure. When the procedure is a module one, the compiler knows everything and checks the call. When it is an external one, the compiler knows only the name unless the program supplies an interface. This lesson writes that interface by hand, then shows the checker working and the silence that follows when the interface is missing.
An INTERFACE block
interface ... end interface holds the declaration of an external procedure: its name, its dummy arguments and their types. It describes the procedure without implementing it, and it must match the real one exactly.
The file explicit.f90 has an external subroutine after the program and the interface that describes it inside:
program interface_demo
implicit none
interface
subroutine show(a, n)
integer, intent(in) :: n
real, intent(in) :: a(n)
end subroutine show
end interface
real :: x(3)
x = [1.0, 2.0, 3.0]
call show(x, 3)
end program interface_demo
subroutine show(a, n)
integer, intent(in) :: n
real, intent(in) :: a(n)
print *, "n =", n, " sum =", sum(a)
end subroutine show
gfortran -Wall -Wextra -std=f2018 explicit.f90 -o explicit
./explicit
n = 3 sum = 6.00000000
The two units are one file here, and gfortran compiles them in one call: the interface is not a second copy of the subroutine but a declaration the program can read. The a(n) of the dummy is the explicit shape of the array lesson, which is the form an interface can describe without a descriptor.
The module gives it for free
Put the same subroutine in a module and the program needs use and nothing else: the interface of every public procedure travels with the module, so the compiler has it at every call. That is the reason a modern Fortran library is a module and not a set of external routines, and the reason the interface block is a tool for old code rather than for new.
The two forms are not equal in what they can describe. A module interface is generated from the procedure and cannot disagree with it; a hand-written interface block can, and a disagreement is a bug that the linker does not see. A program that must interface with an external library writes the block once per procedure and keeps it beside the call.
What the interface catches
With the interface in place the compiler compares every call with it, and a wrong type is an error rather than a wrong answer. The program below passes an integer where the interface says a real array:
program mismatch
implicit none
interface
subroutine show(a, n)
integer, intent(in) :: n
real, intent(in) :: a(n)
end subroutine show
end interface
integer :: i
i = 3
call show(i, 3)
end program mismatch
gfortran -Wall -Wextra -std=f2018 mismatch.f90 -o mismatch
mismatch.f90:12:17:
12 | call show(i, 3)
| 1
Error: Type mismatch in argument 'a' at (1); passed INTEGER(4) to REAL(4)
Four bytes would cross the boundary either way, and the subroutine would read the integer bits as a real. That is the failure without the interface: a number that is not wrong in any way the compiler could see, and a result that makes no sense at all.
EXTERNAL and the old way
external show tells the compiler that the name is a procedure and nothing else: no argument types, no shape, no intent. The call is then built from the actual arguments alone, which is why old Fortran code declares the type of every external function and passes arrays with their size in a separate argument.
The attribute is still legal, and a program that calls a C library function with a matching interface does not need it. What it is not is a substitute for an interface: a declaration that says "there is a procedure called show" cannot catch the call above.
When an interface is required
Some calls cannot be built without an explicit interface at all. An assumed-shape argument needs one, because the argument is passed as a descriptor; a keyword call needs one, because the names come from it; an optional argument needs one, because the caller has to know it may be left out; a function with an array result needs one; and a procedure that is passed as an argument needs one. The error of the compiler in each case names the reason, as the assumed-shape lesson showed.
The practical rule is short: put every procedure in a module unless it is a small helper of one program, and let the module supply what an interface block would have to repeat by hand.
Exercises
- Change the dummy of show to a(:) in both the interface and the subroutine. Does the program still build, and why?
- Remove the interface block from explicit.f90. Does the call still build? What does -Wall say?
- Move show into a module and delete the interface. Which lines disappear from the program?
- Pass a real scalar where the interface expects the array. What does the compiler report now?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does an INTERFACE block describe?
Answer
The name, arguments and types of an external procedure
The block is a declaration without an implementation.
-
Where does a module procedure get its explicit interface from?
Answer
From the module, so USE brings it with the name
The module file carries the interface.
-
What does this program print?
program p implicit none interface function half(x) real, intent(in) :: x real :: half end function half end interface print *, half(8.0) end program p function half(x) real, intent(in) :: x real :: half half = x / 2.0 end function halfAnswer
4.00000000
The function halves its argument.
-
What does the compiler report for a wrong argument type with an interface in view?
Answer
Error: Type mismatch in argument, passed INTEGER(4) to REAL(4)
The interface gives the compiler the type to compare.
-
What does
external showdeclare?Answer
That show is a procedure, with no argument information
The attribute is the old, minimal declaration.
-
Which call requires an explicit interface?
Answer
A call that passes a keyword argument
The keyword names come from the interface.
-
Why can a hand-written interface be wrong?
Answer
It is separate from the procedure, so nothing checks the two against each other
A module generates its interface; a block is typed by hand.
-
What does an assumed-shape dummy require?
Answer
An explicit interface
The descriptor is built from the interface.
-
Which is the modern way to share a procedure between programs?
Answer
Put it in a module and USE the module
The module carries the interface and the code together.
-
What is the danger of a call without an interface?
Answer
The arguments are passed by position and type without a check, so a mismatch is silent
Nothing compares the call with the procedure.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev