Allegheny College
Department of Computer Science
CS220 Programming Languages

Fortran 77

 


Table of Contents

  1. History
  2. Basics
  3. How to compile and run programs
  4. Variables
  5. Expressions
  6. Logical Expressions
  7. If-Statements
  8. Loops
  9. Arrays
  10. Function and subroutines
  11. Input and Output
  12. Recursion
  13. Sample Code
  14. Resources

History

  1. A Brief History of Fortran
  2. The IBM 704
  3. What is Fortran?
Back to Top

Basics

[Code Sample 1]

      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

stop
end

[End of Code Sample 1]

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.

c23456789 (This demonstrates column position!)

c The next statement goes over two physical lines
area = 3.14159265358979
+ * r * r

 

  1. Fortran 77 Basics
Back to Top

How to compile and run programs

$g77 filename.for -o filename

$./filename

Back to Top

Variables

Types and declarations

      integer   list of variables... (var1, var2, var3....)
real list of variables
double precision list of variables
complex list of variables
logical list of variables
character list of variables
 
  1. Variables, types, and declarations

 

Back to Top

Expressions

Type conversion

When different data types occur in the same expression, type conversion has to take place, either explicitly or implicitly. Fortran will do some type conversion implicitly. For example,

      real x
x = x + 1

will convert the integer one to the real number one, and has the desired effect of incrementing x by one. However, in more complicated expressions, it is good programming practice to force the necessary type conversions explicitly. For numbers, the following functions are available:

      int
real
dble
ichar
char

The first three have the obvious meaning. ichar takes a character and converts it to an integer, while char does exactly the opposite.

Example: How to multiply two real variables x and y using double precision and store the result in the double precision variable w:

      w = dble(x)*dble(y)

Note that this is different from

      w = dble(x*y)
  1. Expressions and Assignment
Back to Top

Logical Expressions

Logical expressions can only have the value .TRUE. or .FALSE..
A logical expression can be formed by comparing arithmetic expressions using the following relational operators:

      .LT.  meaning    (less than)
.LE. (less than or equal to)
.GT. (greater than)
.GE. (greater than or equal to)
.EQ. (equal to)
.NE. (not equal to)
Logical expressions can be combined by the logical operators .AND. .OR. .NOT.
  1. Logical Expressions
Back to Top

If-Statements

Example of simple if-statement
if (logical expression) then
statements
endif
Example of If-elseif-else block
if (logical expression) then

statements

elseif (logical expression) then
statements
:
:
else
statements
endif
Example of nested if-elseif block
if (x .GT. 0) then
if (x .GE. y) then
write(*,*) 'x is positive and x >= y'
else
write(*,*) 'x is positive but x < y'
endif
elseif (x .LT. 0) then
write(*,*) 'x is negative'
else
write(*,*) 'x is zero'
endif
  1. If-Statements
Back to Top

Loops

do-loops

The do-loop is used for simple counting:

      integer i, n, sum

sum = 0
do 10 i = 1, n
sum = sum + i
write(*,*) 'i =', i
write(*,*) 'sum =', sum
10 continue

The general form of the do loop is:

      do label  index =  initial_value, terminating_value, step_size (implicityly = 1)
statements
label continue

while-loops

basic format:
label if (logical expr) then
statements
goto label
endif
example:
     integer n

n = 1
10 if (n .le. 100) then
n = 2*n
write (*,*) n
goto 10
endif

until-loops

basic format:

label continue
statements
if (logical expr) goto label
  1. Fortran Loops
Back to Top

Arrays

One-dimensional arrays

The one-dimensional array, which is just a linear sequence of elements stored consecutively in memory.  Which can be declared by the following line:

            real array_name(20)

Array elements are indexed from array_name(1) through array_name(20). 

TWO-dimensional arrays

Two dimensional arrays can be declared by:

	data_type array_name(number_of_rows,number_of_columns)
example:
	real A(3,5)
The above statement declares a two-dimensional array of 3 rows and 5 columns, or 3 X 5 = 15 real numbers.
This data structure can be easily visualized through the following table:
   (1,1)  (1,2)  (1,3)  (1,4)  (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)

Two-dimensional arrays may also have indices in an arbitrary defined range. The general syntax for declarations is:

     array_name (low_index1 : hi_index1, low_index2 : hi_index2)

The total size of the array is then

     array_size = (hi_index1 - low_index1+1)*(hi_index2 - low_index2+1)
  1. Arrays
  2. Arrays in Subprograms
Back to Top

Function and subroutines

Built-in Functions (Be careful to match the types of your variables and your functions!)

      abs     absolute value
min minimum value
max maximum value
sqrt square root
sin sine
cos cosine
tan tangent
atan arctangent
exp exponential (natural)
log logarithm (natural)

example:    x = cos(pi/3.0)

Fortan Functions

A fortan function has to be declared with the correct type in the calling program unit. The function is then called by simply using the function name and listing the parameters in parenthesis.  A function  returns only 1 value.

example:

 program main
real function_name, t, sum
integer m

read (*,*) t
sum = 0.0
do 10 m = 1, 12
sum = sum + funtion_name(m, t)
10 continue
write (*,*) 'Annual rainfall is ', sum, 'inches'

stop
end
      real function function_name(m,t)
integer m
real t

funtion_name = 0.1*t * (m**2 + 14*m + 46)
if (function_name .LT. 0) function_name = 0.0

return
end
Fortran Functions:

Subroutines

A function in Fortan is used to return 1 and only 1 value.  However subroutines can be used to return 0 or more values. 

example:

  1. Fortran Functions & Subroutines
Back to Top

Input and Output

Read is used for input, while write is used for output.

      read (unit_number, format_number) list-of-variables
write(unit_number, format_number) list-of-variables
	unit_number --> Will refer to standard i/o or a file
fomrat_number --> Will refer to a label or format statement
list-of-variables --> a comma delimited list of variables.
example using standard i/o:
      read (*,*) list-of-variables
write(*,*) list-of-variables
or alternatively you can use the following format to read/write from standard i/o:
      read  *, list-of-variables
print *, list-of-variables

 

  1. Simple I/O
  2. Format Statements
  3. File I/O
Back to Top

Recursion

  1. Recursion with Fortran
Back to Top

Sample Code


    pythag.for Simple program for testing Pythagorean theorem
    pythag.out A few sample runs of pythag.for
    names.for Demonstration of string variables
    names.out Sample run of names.for
    compound.for Simple program for computing interest compounded annually
    compound.out Sample run of compound.for
    dice.for Simulates rolling a pair of dice; uses a function to simulate random roll of a die
    dice.out Sample run of dice.for
    piglatin.for Converts a lowercase English word into Pig Latin
    piglatin.out A few sample runs of piglatin.for
    reverse.for Uses a subroutine to reverse the elements in several arrays
    reverse.out Sample run of reverse.for
    nested.for Shows nested loops and two-dimensional arrays
    nested.out Sample run of nested.for
Back to Top

Resources

  1. Fortran Tutorial
  2. CS220, Spring 2003
  3. USER NOTES ON FORTRAN PROGRAMMING (UNFP)
  4. Allan Miller's Fortran Software
  5. Fortran Source Check
  6. The Fortran Saga
  7. Fortran Standards Documents
  8. Fortran Language
  9. A Portable Fortran 77 Compiler
  10. Fortran, Wikipedia
  11. Fortan 77 Tutorial
  12. More Fortran Resources
Back to Top

Matthew T. Engel
Systems Administrator
Allegheny College
Department of Computer Science
Meadville, PA 16335
Revised: 02/02/05.