Fortran 77 Tutorial PDF
Fortran 77 Tutorial PDF
1. Preface
The goal of this Fortran tutorial is to give a quick introduction to the most common
features of the Fortran 77 programming language. It is not a complete reference! Many
details have been omitted. The presentation focuses on scientific computations, mainly
linear algebra. The outline of this tutorial was inspired by the excellent book "Handbook
for Matrix Computations" by T.F. Coleman and C. Van Loan, published by SIAM
(unfortunately this book is out of print).
2. What is Fortran?
Fortran is a general purpose programming language, mainly intended for mathematical
computations in e.g. engineering. Fortran is an acronym for FORmula TRANslation, and
was originally capitalized as FORTRAN. However, following the current trend to only
capitalize the first letter in acronyms, we will call it Fortran. Fortran was the first ever
high-level programming languages. The work on Fortran started in the 1950's at IBM
and there have been many versions since. By convention, a Fortran version is denoted
by the last two digits of the year the standard was proposed. Thus we have
* Fortran 66
* Fortran 77
* Fortran 90 (95)
The most common Fortran version today is still Fortran 77, although Fortran 90 is
growing in popularity. Fortran 95 is a revised version of Fortran 90 which is expected to
be approved by ANSI soon (1996). There are also several versions of Fortran aimed at
parallel computers. The most important one is High Performance Fortran (HPF), which
is a de-facto standard.
Users should be aware that most Fortran 77 compilers allow a superset of Fortran 77,
i.e. they allow non-standard extensions. In this tutorial we will emphasize standard ANSI
Fortran 77.
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
stop
end
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
declarations
statements
stop
end
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:
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.
Comments
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).
Continuation
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:
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.
The list of variables should consist of variable names separated by commas. Each
variable should be declared exactly once. If a variable is undeclared, Fortran 77 uses a
set of implicit rules to establish the type. This means all variables starting with the letters
i-n are integers and all others are real. Many old Fortran 77 programs uses these
implicit rules, but you should not! The probability of errors in your program grows
dramatically if you do not consistently declare your variables.
Integers and floating point variables
Fortran 77 has only one type for integer variables. Integers are usually stored as 32 bits
(4 bytes) variables. Therefore, all integer variables should take on values in the range [-
m,m] where m is approximately 2*10^9.
Fortran 77 has two different types for floating point variables, called real and double
precision. While real is often adequat, some numerical calculations need very high
precision and double precision should be used. Usually a real is a 4 byte variable and
the double precision is 8 bytes, but this is machine dependent. Some non-standard
Fortran versions use the syntax real*8 to denote 8 byte floating point variables.
program circle
real r, area, pi
parameter (pi = 3.14159)
stop
end
* The "variable" defined in the parameter statement is not a variable but rather a
constant whose value can never change
* A "variable" can appear in at most one parameter statement
* The parameter statement(s) must come before the first executable statement
1
0
-100
32767
+15
1.0
-0.25
2.0E6
3.333E-1
The E-notation means that you should multiply the constant by 10 raised to the power
following the "E". Hence, 2.0E6 is two million, while 3.333E-1 is approximately one third.
Get Complete Document At :
http://allbookserve.org/academi/fortran-formula-translation/fortran-77%20tutorial.pdf