Program structure

Contents
Program units
host association
Order of statements
Style that scales

Program units

Fortran code is organized in program units: the main program, modules, subroutines, and functions.

Small scripts need only a program. Larger projects move shared data and procedures into modules that the program loads with use.

module tools implicit none contains subroutine greet() implicit none print *, "Hello from a module" end subroutine greet end module tools

host association

The contains keyword splits a program unit into a declaration part above it and procedures below it.

Procedures below contains can see the names declared above, which is called host association. Use it for small helpers that belong to one program.

program hostdemo implicit none integer :: n n = 3 call show() contains subroutine show() implicit none print *, "n =", n end subroutine show end program hostdemo

Order of statements

Each unit follows the same order: header line, use statements, implicit none, declarations, executable code, then contains with procedures.

Declarations always come before executable statements. The compiler rejects code that mixes them in the wrong order.

program orderdemo implicit none integer :: x x = 10 print *, "x =", x end program orderdemo

Style that scales

Indent the body by two spaces, give procedures clear names, and keep one idea per line.

Place shared constants and helpers in a module early, so the main program stays short and easy to read as the project grows.

Next: Types: integer real complex logical character

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: