Background on Fortran

1. BACKGROUND ON FORTRAN

1.1 Introduction

The language of choice in this textbook is FORTRAN, although some prefer to use C. C and FORTRAN are high-level languages, which serve as translators between the programmer and the Central Processing Unit (CPU) of a computer, which does the "work." This sweeping statement is deserving of some dissection. First, a high-level language enables code to be written in a style of mathematical formulas, instead of machine instructions. Indeed, the name FORTRAN derives from FORmula TRANslation, embodying both notions of formulas and translation. Alternatively, a CPU accepts only low-level instructions. Consider the following example, where the numbers X and Y are to be added, resulting in Z. In FORTRAN, this statement is written
Z = X + Y

FORTRAN translates this statement into low-level machine instructions understandable by the specific CPU on which the code exists. These low-level machine instructions might be:

Fetch the value in memory location X, store it in register 00
Fetch the value in memory location Y, store it in register 01
Add registers 00 and 01, store the resultant in register 02
Store the value in register 02 into memory location Z

It is actually much worse than this, as the low-level machine instructions appearing above, when implemented on a particular machine, are actually encoded as 0's and 1's, and this is not shown. Note how much simpler and intuitive is the FORTRAN statement. Furthermore, the FORTRAN statement is the same for any computer architecture, while the low-level machine instructions differ from one computer to another. Ergo, a single computer code written in FORTRAN can be compiled (this is the act of performing the translation for the specific machine on which the code exists) and then run on any machine, termed "portability." Thus, FORTRAN, or indeed any high-level language, offers the great advantages of: (1) simplicity, and (2) portability.

1.2. History of FORTRAN

FORTRAN was conceived over three years, and delivered in 1957 for the IBM 704 computer by a research team inside IBM headed by John Backus. Before this, instructions were encoded using 0's and 1's and, using two-position switches on the fronts of very large computers, entered directly into memory locations. Over the time FORTRAN was being developed, there was the question of whether the translation could be done efficiently and effectively. So Backus and his group were taking a significant chance by embarking upon the effort. The implementation was very successful, and resulted in the language becoming very popular with engineers and scientists.

Since then, FORTRAN has gone through three major evolutions, each defined by a formal standards committee. A "standard" FORTRAN is agreed upon to ensure that it is compatible across all platforms. In 1966, the first FORTRAN standard emerged, called (not surprisingly) FORTRAN 66. The standard was 36 pages in length (languages were much simpler then).

As computers evolve, so do the requirements for new capabilities in computer languages. In fact, as soon as FORTRAN 66 was delivered, work began upon the next version, FORTRAN 77, delivered a year later than expected in 1978. The FORTRAN 77 standard was 300 pages in length. Much of the improvements were in input/output (I/O), and easier file handling. A third revision got underway as soon as FORTRAN 77 was delivered, Fortran 90 (notice that all but the F in Fortran are now lower case). The new version was accepted by the International Standards Organization in 1990, and by the American National Standards Institute (ANSI) in 1992.

Earlier versions of Fortran were spelled with all capital letters, i.e. FORTRAN IV and FORTRAN 77. However, Fortran 90 is spelled only with a captial "F."

We shall learn Fortran 90 by example through programming. This hands-on approach is much better than a dry exposition of the rules of Fortran. However, we shall learn enough of the rules to become good programmers. First, we'll begin with a few basics and then go through an example.

1.3. Fortran 90

The new version is remarkable in many respects. The unique features of the language resulted directly from the goals for the new version, which were threefold:

These goals were achieved through the implementation of a whole host of new features:

Most notable of the new features may perhaps be the array processing, where whole array operations are done with a single instruction (looping is automatic). This permits automatic parallelization, suitable for the new parallel computer architectures. Another outstanding feature is the free-form source code, whereby statement numbers, continuations and statements no longer must occupy specific columns in a file. The most difficult errors to divine and those made all too frequently made by new users, in the author's experience, were those where one typed a variable name past column 72, or started a statement before column 6. We urge you to explore the rich and robust full language of Fortran 90, in your studies of this textbook.

1.4. Fortran or C?

Taking a stance on this issue is akin to taking sides in a religious war. This is especially so, as the language C has many devoted followers, both because it is a good language, and perhaps because Fortran 90 was late in coming. Many were loathe to continue to use the dated language FORTRAN 77 through the late 1980's, and instead turned to C.

Some observations about the languages relative to each other are offered here, in an attempt to add perspective. First, it is apparent that the languages are growing together. Fortran 90 is very, very C-like. This is good in that it narrows the gap in the capabilities of the languages with advanced features from which both languages benefit. However, this seems in a sense to have made the arguments more focused and poignant.

One notable attribute of C is that typically about 95% of most operating systems are written in C. Thus, C is a much better language for interacting with and writing operating systems. C also seems to be the preferred language for computer graphics, as most graphics subroutines are written in C. Further, C seems to be better than Fortran for dealing with character data (text) or purely integer data. Thus, C seems to be the preferred language of computer scientists, who typically deal with these issues. Finally, object-oriented C++ seems to be emerging as the next new promising language, offering many of the same advantages of Fortran array processing, in an even more flexible fashion. However, we have observed that C, being too forgiving in the mistakes the user is allowed to make and in the flexibility with which the same resultant can be expressed, is a bit more difficult to learn than Fortran, but this probably depends more on the instructor than the language.

On the other hand, Fortran seems to be much better at I/O, numerics and parallelization. Thus, Fortran seems better suited for performing massive computations, such as done by modern computational scientists. It is most certainly true that one can be very productive in either language. If one is facile with either language, there is probably little of a compelling nature to motivate that the other language be learned.

Waxing philosophical for the nonce, it may be that we will all be programming in Javascript, and eschew both languages. Let the newest religious war begin...

1.5. Acknowledgment

Much of the background material for this section was abstracted from presentations and personal conversations the author had with Jeanne Adams, who Chaired the Fortran 90 standards committee from 1977 to 1992. The author is greatly indebted to Jeanne, both for her excellent historical perspective, and for providing the world with a truly wonderful language.

1.6. References

The Fortran 90 standard can be obtained from:

International Organization for Standards
1,rue de Varembe
case postale 56
CH-1211 Geneva 20
Switzerland
$150 (prices as of 1994)

American National Standards Institute
1430 Broadway
New York, NY 10018
$150

Unicomp, Inc.
1875 San Bernadino Ave., NE
Albuquerque, NM 87122
(505) 275-0800
$125 for a PostScript file, plus $10 for each copy made

2. PREPARING PROGRAMS

Preparing programs is much like living life. When faced with a task beyond our comprehensive abilities, we break it up into smaller tasks - each of which we can easily assimilate. In the case of programming, this is called the "top-down" approach. In this style, we break the programming task down into separate pieces or tasks.

Note that we sometimes have to break down these tasks into even smaller tasks. For very complicated problems, we may have several levels of tasks - each more specific and detailed than the previous. Thus, the logical structure of approaching problems is akin to Unix's directory structure - a tree.

At times, the above approach is cumbersome (especially when we have to jump between levels). Nevertheless, it is a very good way to learn programming, and we shall adopt it, but not be constrained by it.

First, let us look at the structure of a program.

3. PROGRAM STRUCTURE

Second, let us cover the possible types of tasks that can be performed on a computer.

4. FUNDAMENTAL PROGRAMMING CONTROL CONSTRUCTS

There are only three fundamental programming control constructs. Into these three control constructs, we must fit all of our tasks. The three constructs are:

4.1 Sequential execution

(do this)

(then do this)

(then do this)

.

.

.

4.2 Branching

If (this) then

(do this)

elseif (this) then

(do this)

elseif (this) then

.

.

else

(do this)

endif

4.3 Looping

do until finished:

(this)

(and this)

(and this)

.

.

.

end do

5. EXAMPLE PROGRAM - AVERAGE VALUE

Program Compute

    ! This program computes the
    ! average of a set of experimental
    ! data values.
  INTEGER :: COUNT
  REAL :: X, SUM, AVERAGE
  SUM = 0.0
  COUNT = 0
  READ *, X
    1 IF (X /= 0.0) THEN
   SUM = SUM + X
   COUNT = COUNT + 1
   READ *, X
   GO TO 1
  ENDIF
  AVERAGE = SUM/REAL(COUNT)
  PRINT *,'The average is',AVERAGE
  END

Example: Hand Computation

The average value is the sum of all the numbers divided by the number of numbers. In this case, the average value is: (14.1 + 21.63 + 57)/3 = 92.73/3 = 30.91

Example: Computer Algorithm Initially

SUM = 0.0

COUNT = 0

program reads 14.1

SUM = 0.0 + 14.1

COUNT = 1

program reads 21.63

SUM = 14.1 + 21.63

COUNT = 2

program reads 57.

SUM = 35.73 + 57.

COUNT = 3

program reads 0

SUM = 92.73

COUNT =3

AVERAGE = SUM/REAL(COUNT) = 92.73/3.

program prints average

The average is 30.91

6. FEATURES

Some of the features that you may use in your programming are the following:

6.1 Variable Declarations

6.2 Line structure

Fortran 90 offers a major structural change from Fortran77. In the past, Fortran instructions had to be placed in particular columns (7-72), while the other columns were reserved for special purposes. This is referred to as "fixed form," and will continue to be accepted by Fortran 90 compilers should you choose to use this format. The new code style offered by Fortran 90 is called "free form," and places relatively few constraints on the program code. In free form, the column dependency disappears entirely. Here are the basic rules that must be followed:

As an example, refer to the previous example, Program Compute.

6.3 Numerical operations

6.4 Logical functions

6.5 Input/Output (I/O)

read *, variable list (commas are used as separators)

Example: read *, a, b, c

print *, variable list (commas are used as separators)

Example: print *,'a, b, and c=',a, b, c

Note: in output, anything between ' ' is a literal (printed out exactly as it appears).

7. ASSIGNMENT STATEMENT

In Fortran, = is different than it is in mathematics. In Fortran, it means complete the calculation on the right-hand side (RHS) of the =, and store the single, resulting number in the storage location (memory) reserved for the variable appearing on the LHS. Therefore, statements in Fortran with = are refered to as "Assignment Statements."