Explicit interfaces

Contents
Why interfaces matter
Modules give them free
Interface blocks
Catch mismatches

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 implicit none use stats print *, double_it(21) end program 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 implicit none use mtools print *, triple(14) end program 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

./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'

Next: Derived types

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: