3. Fortran 77 Basics

A Fortran program is just a sequence of lines of text. The text has to follow a certain syntax to be a valid Fortran program. We start by looking at a simple example:
      program circle
      real r, area
c This program reads a real number r and prints
c the area of a circle with radius r.
      write (*,*) 'Give radius r:'
      read  (*,*) r
      area = 3.14159*r*r
      write (*,*) 'Area = ', area
The lines that begin with with a "c" are comments and has no purpose other than to make the program more readable for humans. Originally, all Fortran programs had to be written in all upper-case letters. Most people now write lower-case since this is more legible, and so will we.

Program organization

A Fortran program generally consists of a main program (or driver) and possibly several subprograms (or procedures or subroutines). For now we will assume all the statements are in the main program; subprograms will be treated later. The structure of a main program is:
      program name



In this tutorial, words that are in italics should not be taken as literal text, but rather as a generic description. The stop statement is optional and may seem superfluous since the program will stop when it reaches the end anyways, but it is recommended to always terminate a program with the stop statement to emphasize that the execution flow stops there.

Column position rules

Fortran 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules:
Col. 1    : Blank, or a "c" or "*" for comments
Col. 2-5  : Statement label (optional)
Col. 6    : Continuation of previous line (optional)
Col. 7-72 : Statements
Col. 73-80: Sequence number (optional, rarely used today)
Most lines in a Fortran 77 program starts with 6 blanks and ends before column 72, i.e. only the statement field is used. Note that Fortran 90 allows free format.


A line that begins with the letter "c" or an asterisk in the first column is a comment. Comments may appear anywhere in the program. Well-written comments are crucial to program readibility. Commercial Fortran codes often contain about 50% comments. You may also encounter Fortran programs that use the exclamation mark (!) for comments. This is highly non-standard in Fortran 77, but is allowed in Fortran 90. The exclamation mark may appear anywhere on a line (except in positions 2-6).


Occasionly, a statement does not fit into one single line. One can then break the statement into two or more lines, and use the continuation mark in position 6. Example:
c23456789 (This demonstrates column position!)

c The next statement goes over two physical lines
      area = 3.14159265358979
     +       * r * r
Any character can be used instead of the plus sign as a continuation character. It is considered good programming style to use either the plus sign, an ampersand, or numbers (2 for the second line, 3 for the third, and so on).

Blank spaces

Blank spaces are ignored in Fortran 77. So if you remove all blanks in a Fortran 77 program, the program is still syntactilly correct but almost unreadable for humans.


Exercise A
Identify at least 3 errors in the following Fortran 77 program:
c23456789 (This demonstrates column position!)
     integer int
     int = 12
     write(*,*) 'The value of int is',
+    int

[AMS Home Page] [Fortran Tutorial Home]