Modules

Contents
Define a module
Use it
Public vs private
Compile order

Define a module

A module is a package of procedures and data: it opens with module, shares its contents after contains, and closes with end module.

Everything inside one module sees everything else, and every user of the module gets an explicit interface for free.

module stats implicit none contains integer function double_it(x) implicit none integer, intent(in) :: x double_it = x * 2 end function double_it integer function triple(x) implicit none integer, intent(in) :: x triple = x * 3 end function triple end module stats

Use it

The use statement imports a module into a program: after use stats every public procedure is callable by name.

Keep the module in stats.f90 and the program in main.f90, then build both files together.

program usedemo implicit none use stats print *, double_it(20) print *, triple(14) end program usedemo

gfortran stats.f90 main.f90 -o demo && ./demo

40
42

Public vs private

Private hides the helpers while public opens the front door: start with private, then export only the names callers need.

Callers of vault can reach open_it but cannot touch secret, so internals stay free to change.

module vault implicit none private public :: open_it contains subroutine open_it() implicit none print *, "open" end subroutine open_it subroutine secret() implicit none print *, "shut" end subroutine secret end module vault

Compile order

Modules must be compiled before their users: list the provider file first so the module file is ready when the user needs it.

Reversed order fails fast with a missing module file error, which points straight at the ordering mistake.

program vaultdemo implicit none use vault call open_it() end program vaultdemo

gfortran main.f90 stats.f90 -o demo

Fatal Error: Cannot open module file 'stats.mod' for reading

Next: USE with ONLY

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: