
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.
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.
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.
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...
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
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.
Second, let us cover the possible types of tasks that can be performed on a computer.
There are only three fundamental programming control constructs. Into these three control constructs, we must fit all of our tasks. The three constructs are:
(do this)
(then do this)
(then do this)
.
.
.
If (this) then
(do this)
elseif (this) then
(do this)
elseif (this) then
.
.
else
(do this)
endif
do until finished:
(this)
(and this)
(and this)
.
.
.
end do
! 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
Some of the features that you may use in your programming are the following:
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.
read *, variable list (commas are used as separators)
Example:
print *, variable list (commas are used as separators)
Example:
Note: in output, anything between ' ' is a literal (printed out exactly as it appears).
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."
