100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Fortran Program Structure

How a Fortran program is organized, from the program statement and implicit none through declarations, executable statements, and program units.

FoundationsBeginner9 min readJul 10, 2026
Analogies

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.

fortran
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

Was this page helpful?

Topics covered

#Programming#FortranStudyNotes#FortranProgramStructure#Fortran#Program#Structure#Implicit#StudyNotes#SkillVeris#ExamPrep