Fortran Program Structure
Every Fortran program is organized as a sequence of program units, the most common being the main program, subroutines, functions, and modules. A minimal main program begins with the program keyword followed by a name, contains a declaration section for variables, an executable section with statements, and ends with the end program statement. This clear separation between declarations and executable code makes Fortran programs easy to scan and reason about, even in large scientific codebases.
Cricket analogy: Like a Test match structured into innings with a clear start (toss) and end (declaration or all-out), a Fortran program has a clear program...end program boundary framing everything inside it.
implicit none and Declarations
Every well-written modern Fortran program should include the statement implicit none immediately after the program line. Historically, Fortran allowed variables starting with letters I through N to be implicitly typed as integers and all others as real numbers, a legacy feature that causes silent bugs from simple typos. Adding implicit none disables this behavior and forces every variable to be explicitly declared with a type such as integer, real, character, or logical before use.
Cricket analogy: Like insisting on DRS review for every close decision instead of trusting the on-field umpire's snap judgment, implicit none forces every variable to be explicitly declared instead of trusting Fortran's old implicit-typing guesswork.
Program Units: Subroutines, Functions, and Modules
Beyond the main program, Fortran code is organized into subroutines (called with the call keyword, which perform an action but do not directly return a value through their name), functions (which compute and return a single value), and modules (which group related declarations, types, and procedures for reuse via the use statement). Modern Fortran style strongly favors putting subroutines and functions inside modules rather than as free-standing external procedures, because module procedures get compile-time interface checking that catches argument mismatches.
Cricket analogy: Like a team having specialist bowlers, batsmen, and an all-rounder each with a distinct role, Fortran separates responsibilities into subroutines, functions, and modules, each with a distinct role.
Modern Fortran style guides strongly recommend always writing implicit none and placing procedures inside modules. Compiling with a flag like gfortran -Wall -fimplicit-none will warn you if you forget it.
program structure_demo
implicit none
integer :: count
real :: total
count = 5
total = 12.5
call show_result(count, total)
contains
subroutine show_result(n, t)
integer, intent(in) :: n
real, intent(in) :: t
print *, "Count:", n, "Total:", t
end subroutine show_result
end program structure_demo- A Fortran main program is bounded by a program statement and a matching end program statement.
- The declaration section (variable types) always comes before the executable section (statements).
- implicit none should be included in every program to disable Fortran's legacy implicit typing rules.
- Subroutines perform actions and are invoked with call; functions compute and return a single value.
- Modules group related declarations and procedures together and are imported with the use statement.
- Placing procedures inside modules enables compile-time interface checking of arguments.
- The contains keyword introduces internal procedures within a program unit, as shown with subroutines.
Practice what you learned
1. What statement should appear immediately after the program line in every well-written Fortran program?
2. Without implicit none, what type does Fortran implicitly assign to a variable named 'kappa'?
3. Which keyword is used to invoke a subroutine in Fortran?
4. What is the primary benefit of placing subroutines and functions inside a module rather than as external procedures?
5. Which keyword introduces internal procedures within a Fortran program unit?
Was this page helpful?
You May Also Like
What Is Fortran?
An introduction to Fortran, the pioneering language built for numerical and scientific computing that still powers weather models and supercomputers today.
Fortran Data Types and Variables
An overview of Fortran's intrinsic data types, integer, real, character, logical, and complex, and how to declare and initialize variables.
Your First Fortran Program
A hands-on walkthrough of writing, compiling, and running a simple Fortran program, from source file to executable.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics