Program Structure & Your First Program

3.1 Hello World

program hello
  implicit none
  print *, "Hello, World!"
end program hello
gfortran hello.f90 -o hello
./hello

3.2 Anatomy of a program

Program units: main program, modules, submodules, external procedures, block data (obsolete).

program circle_area               ! program unit name
  implicit none                   ! ALWAYS: disable implicit typing
  ! --- declarations ---
  real, parameter :: pi = 3.14159265
  real :: radius, area
  ! --- executable statements ---
  print *, "Enter radius:"
  read *, radius
  area = pi * radius**2
  print *, "Area = ", area
contains                          ! internal procedures (optional)
  ! subroutines/functions here
end program circle_area

3.3 implicit none

Without it, variables starting with i–n are implicitly integer and all others real — a source of countless bugs (typos create new variables). Always write implicit none (Fortran 2018 also allows implicit none (type, external)).

3.4 Source form

Free form (.f90, .f95, .f03, .f08) Fixed form (.f, .for, .f77) any column, up to 132 chars columns 1–5 labels, 6 continuation, 7–72 code ! comments anywhere C or * in column 1 & at line end for continuation any char in column 6

Use free form for all new code. File extension .f90 is conventional for any modern free-form source. Uppercase extensions (.F90) mean "run the C preprocessor first".

3.5 Syntax basics

x = 1.0; y = 2.0                  ! semicolons separate statements on one line
total = a + b + &                 ! & continues a line
        c + d
! Fortran is case-insensitive: PRINT, Print, print are identical
! Names: letters, digits, underscore; start with a letter; up to 63 characters