Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 39 ยท Getting Started

Your First Fortran Program

Write and understand your first Fortran program: program, implicit none, declarations, assignments, print and write, comments, and a simple area calculator.

Every language starts with a program that prints a greeting, but a Fortran "Hello, World" is also a good vehicle for the habits you will use in every program: name the program, switch off implicit typing, declare variables, compute, print. This lesson walks through a complete program line by line and then grows it into a small calculator.

The smallest useful program

FORTRAN
program hello
  implicit none
  print *, "Hello, World!"
end program hello

Compile with gfortran hello.f90 -o hello and run ./hello. Output:

TEXT
 Hello, World!

Line by line:

  1. program hello begins the main program and gives it a name. The name is optional, but always use it.
  2. implicit none switches off Fortran's old habit of guessing a variable's type from its first letter. With it, every variable must be declared, and misspelled names become compile errors instead of silent bugs. Write it in every program unit.
  3. print *, "Hello, World!" writes to the screen. The * means "default format", the comma separates it from the list of things to print.
  4. end program hello ends the program. Repeating the name is optional but helps in long files.

Strings can use single or double quotes: 'Hello' and "Hello" are equivalent. To put a quote inside, double it: 'It''s fine'.

Variables and assignment

Declarations come before the statements that use them:

FORTRAN
program area
  implicit none
  real :: width, height, area_value

  width = 4.5
  height = 2.0
  area_value = width * height
  print *, "Area =", area_value
end program area

The :: separates the type from the names. real is a floating-point number; width = 4.5 is an assignment, which stores a value (Fortran uses a single = for assignment and == for comparison). Notice that a variable is named area_value, not area: a program and a variable may not share a name in the same scope, and underscores are allowed in names.

The output is a line starting with a space, the text, and the number printed in a default width. The exact number of spaces and digits depends on the compiler, so rely on the next lessons' formatting when you need it exact. For these inputs the value is 9.0, shown like 9.00000000 with gfortran.

Python Stand-in (Python): the area calculation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Comments

A ! starts a comment that runs to the end of the line. It can follow code:

FORTRAN
program commented
  implicit none
  ! This whole line is a comment
  real :: speed   ! in meters per second

  speed = 12.5    ! assign a value
  print *, speed
end program commented

There are no block comments in Fortran; comment several lines by starting each with !. A ! inside a string is just a character.

Reading input

A program becomes more useful when it asks for numbers. read *, a, b reads values from the keyboard, separated by spaces, commas or new lines:

FORTRAN
program rectangle
  implicit none
  real :: w, h

  print *, "Enter width and height:"
  read *, w, h
  print *, "Area:", w * h
  print *, "Perimeter:", 2.0 * (w + h)
end program rectangle

If the user types 3 5, the program prints an area of 15 and a perimeter of 16 (with default formatting adding trailing zeros). Notice you can print an expression directly; no temporary variable is needed.

Python Stand-in (Python): input and expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting several statements on a line

A newline ends a statement. To put two statements on one line use ;, and to continue a long statement onto the next line end the first line with &:

FORTRAN
program continued
  implicit none
  real :: a, b
  a = 1.0; b = 2.0
  print *, "The sum of a and b is", &
           a + b
end program continued

Use ; sparingly, because one statement per line is easier to read. Continuation lines are common for long formulas and argument lists. The default line limit is 132 characters, so wrap well before that.

Your program as a pipeline

flowchart LR
  A["declare<br/>variables"] --> B["read or set<br/>values"]
  B --> C["compute"]
  C --> D["print<br/>results"]

Nearly every beginner program follows this four-step shape.

Common beginner mistakes

  • Forgetting implicit none. A typo like are_value then silently creates a new real or integer variable.
  • Declaring after statements. All declarations must come before the first executable statement in a program unit (a block construct is an exception, covered later).
  • Using == for assignment or = for comparison. x = 5 assigns; x == 5 tests.
  • Mismatched program names. end program wrong_name is an error if the name does not match.
  • Integer division surprises. 1 / 2 is 0. Write 1.0 / 2.0 when you want 0.5 (covered in operators and expressions).
Python Stand-in (Python): integer vs real division

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does implicit none do?

    implicit none disables type guessing from the first letter, so undeclared variables become compile errors.
  2. 2. Which character starts a comment in Fortran?

    An exclamation mark starts a comment that runs to the end of the line.
  3. 3. How do you continue a statement onto the next line in free-form source?

    A trailing & means the statement continues on the next line.
  4. 4. What is the value of 7 / 2 when both numbers are integers?

    Integer division discards the fractional part, so 7 / 2 is 3.

Summary

  • A program is program name ... end program name.
  • Put implicit none first and declare every variable with type :: name.
  • = assigns; print * and read * use default formatting.
  • ! starts a comment, & continues a line, ; separates statements on one line.
  • Integer division truncates, so use real numbers when you need fractions.

Next: Program Structure and Source Form