Fortran 90/95 Quick Reference Guide

New to Fortran? Whether you're a scientist, engineer, or curious programmer, learning Fortran opens the door to decades of high-performance
scientific computing. This guide walks you through the complete workflow—from writing your first program to compiling with gfortran and running it on Linux.

“How It Works” – The Mental Model

Fortran works like this: You write instructions in a .f90 file → The compiler (gfortran) translates them to machine code → You run the resulting executable (./program_name).

Key Concept: Fortran is compiled, not interpreted. This means:

  1. You write code → Save as .f90
  2. You compile → gfortran file.f90 -o program
  3. You run → ./program

📋 Quick Syntax Reference

Program Structure (MANDATORY!)

fortran

program program_name          ! MUST HAVE - Start
  implicit none               ! RECOMMENDED - Catches errors
  ! Your code here
  print *, "Hello"            ! Print to screen
end program program_name      ! MUST HAVE - End

Variables & Data Types

fortran

! Integer (whole numbers)
integer :: x, y
integer :: z = 10

! Real (decimal numbers)
real :: a, b
real :: pi = 3.14159

! Double precision (more accurate)
double precision :: d, e

! Character (text)
character(len=20) :: name
character(len=*), parameter :: greeting = "Hello"

! Logical (true/false)
logical :: flag
flag = .true.
flag = .false.

! Arrays
real, dimension(5) :: arr           ! 1D array size 5
real, dimension(3,3) :: matrix      ! 2D array 3x3
real :: arr2(5)                     ! Alternative syntax

Math Operations

fortran

! Basic math
x = 5 + 3      ! Addition
x = 5 - 3      ! Subtraction
x = 5 * 3      ! Multiplication
x = 5 / 3      ! Division (integer gives integer)
x = 5.0 / 3.0  ! Real division
x = 5 ** 2     ! Power (5² = 25)

! Order of operations: ( ) first, then **, then * /, then + -
! Example: x = (5 + 3) * 2 ** 2  ! = (8) * 4 = 32

Conditional Statements

fortran

! If statement
if (x > 0) then
  print *, "Positive"
else if (x < 0) then
  print *, "Negative"
else
  print *, "Zero"
end if

! Select case (like switch)
select case (x)
  case (1)
    print *, "One"
  case (2, 3, 4)
    print *, "Two, three, or four"
  case default
    print *, "Other"
end select

Loops

fortran

! DO loop (count-controlled)
do i = 1, 10
  print *, i
end do

! DO with step
do i = 1, 10, 2    ! 1, 3, 5, 7, 9
  print *, i
end do

! DO WHILE loop
do while (x < 10)
  x = x + 1
end do

! Exit loop early
do i = 1, 100
  if (i > 10) exit    ! Stop at 10
  print *, i
end do

Arrays (MOST IMPORTANT FOR SCIENTIFIC COMPUTING)

fortran

! Array operations (vectorized!)
real, dimension(5) :: a, b, c
a = [1.0, 2.0, 3.0, 4.0, 5.0]
b = [5.0, 4.0, 3.0, 2.0, 1.0]
c = a + b        ! Whole array operation!

! Array slices
a(3:5) = 0       ! Set elements 3-5 to 0
a(1:3) = [1,2,3] ! Set first 3 elements

! 2D arrays
real :: matrix(3,3)
matrix(1,1) = 1.0   ! First row, first column
matrix(:,1) = 1.0   ! Entire first column
matrix(2,:) = 0.0   ! Entire second row

Subprograms (Functions & Subroutines)

fortran

! Function (returns a value)
function add(a, b) result(result)
  implicit none
  real, intent(in) :: a, b
  real :: result
  result = a + b
end function add

! Subroutine (doesn't return a value)
subroutine print_sum(a, b)
  implicit none
  real, intent(in) :: a, b
  print *, "Sum:", a + b
end subroutine print_sum

! Calling them
x = add(5.0, 3.0)
call print_sum(5.0, 3.0)

Modules (Reusable Code)

fortran

module my_module
  implicit none
  contains
    function add(a, b)
      real :: add
      real, intent(in) :: a, b
      add = a + b
    end function add
end module my_module

! Use in main program
program main
  use my_module
  implicit none
  print *, add(5.0, 3.0)
end program main

🔑 Key Rules to Remember

RuleWhyExample
Every program needs program and end programTells compiler where code starts/endsprogram hello ... end program hello
Use implicit noneForces you to declare variables → catches typos!implicit none at top
Strings MUST be in quotes"text" or 'text'print *, "Hello"
Comments start with !Everything after ! is ignored! This is a comment
Arrays start at index 1NOT 0! Unlike Python/Carr(1) is first element
Use :: when declaringSeparates type from variablesinteger :: x, y
Indentation matters for readabilityNOT required, but helps humans!Indent code inside loops/ifs

💡 Common Commands (Bash Reference)

CommandPurpose
nano file.f90Open file in editor
gfortran file.f90 -o programCompile (create executable)
./programRun the program
gfortran -O2 file.f90 -o programCompile with optimization (faster!)
gfortran file.f90 -o program -llapack -lblasLink with BLAS/LAPACK
cat file.f90View file contents
lsList files

📚 Learning Path

Starter Programs (Learn Syntax)

  1. hello.f90 → Program structure
  2. variables.f90 → Data types
  3. loops.f90 → DO loops
  4. arrays.f90 → Array operations
  5. functions.f90 → Subprograms

Advanced (Scientific Computing)

  1. solve_linear.f90 → LAPACK for linear systems
  2. matrix_multiply.f90 → BLAS for matrix multiplication
  3. eigenvalues.f90 → LAPACK for eigenvalues

📝 Quick Reference Card

text

┌──────────────────────────────────────────────────────────┐
│                    FORTRAN 90/95 CHEAT SHEET            │
├──────────────────────────────────────────────────────────┤
│ PROGRAM STRUCTURE                                       │
│ program name                                            │
│   implicit none                                         │
│   ! code here                                           │
│ end program name                                        │
├──────────────────────────────────────────────────────────┤
│ DATA TYPES                                              │
│ integer, real, double precision, character, logical     │
│ integer :: x                                            │
│ real, dimension(10) :: arr                              │
├──────────────────────────────────────────────────────────┤
│ CONTROL FLOW                                            │
│ if (condition) then                                     │
│   ! code                                                │
│ else if (condition) then                                │
│   ! code                                                │
│ else                                                    │
│   ! code                                                │
│ end if                                                  │
│                                                         │
│ do i = 1, 10                                            │
│   ! code                                                │
│ end do                                                  │
├──────────────────────────────────────────────────────────┤
│ PRINTING                                                │
│ print *, "Text", variable                               │
│ print '(F10.4)', x    ! formatted output                │
├──────────────────────────────────────────────────────────┤
│ COMPILING                                               │
│ gfortran file.f90 -o program                            │
│ gfortran -O2 file.f90 -o program -llapack -lblas       │
├──────────────────────────────────────────────────────────┤
│ RUNNING                                                 │
│ ./program                                               │
└──────────────────────────────────────────────────────────┘

🎯 Featured Resource Entry

When I return to practice Fortran, look for:

Study Resources:

  1. Fortran 90 Tutorial (Stanford) → Beginner-friendly, covers basics
  2. Fortran Wiki → Comprehensive reference, community-driven
  3. Quickstart Tutorial → Hands-on, practical examples

Practice Flow:

  1. Write code in nano or your preferred editor
  2. Compile with gfortran file.f90 -o program
  3. Run with ./program
  4. Check output, debug, repeat