Chapter 1
Essentials of Fortran-90/95/2003
Background and Introduction

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 60 Homepage | Return to Brief Contents Page]
[Return to Full Contents Page]

Contents


1.1 History

FORTRAN is an acronym for FORmula TRANslation. This scientific computer language was developed in the early 1950's by John W. Backus of the International Business Machine Corporation (IBM) in San Jose, California as an easier-to-use alternate to coding programs in what is now called "Assembly Language." It was designed as a relatively simple language that could translate algebraic formulas such as, a = b + c / d into computer code, such as,
     A = B + C / D 
Since its original definition in 1955, Fortran has gone through a number of revisions, and today there are several versions of Fortran in use.

The combination of early simple rules (which are, in general, still in effect) with newer features gives a certain simplicity to basic Fortran programs. For example, the following C++ code

    #include <iostream.h>
    int main ()
       {
          cout << "Hello, world!" << endl;
	  return 0;
       }
could be rendered in the latest version of Fortran as:
     program test1
       write(*,*) "Hello, world!"
     end

The major dialects currently in use are called FORTRAN-77 and Fortran-90/95. An earlier version, called FORTRAN IV (or FORTRAN-66), appeared in 1966 followed by FORTRAN-77 in 1977. In 1992 a new version of Fortran was approved incorporating many new features, and this version was called Fortran-90. Certain minor changes were agreed upon in 1995 resulting in Fortran-95. The last revision was published in 2003 and is commonly called "Fortran-2003." It may, however, take a few years until Fortran 2003 features will become readily available in compilers. Since the differences between Fortran-90 and Fortran-95 are so few and minor, these versions are often referred to by the one name of "Fortran-90/95." Fortran-2003 has added several new features, noted later, but has not substantially altered the basic revised version introduced as Fortran-90. For that reason, the text will usually refer to the current version of Fortran as "Fortran-90/95/2003," except when specifically referring to the older version of the language or to new features introduced with Fortran-2003.

One minor change introduced with Fortran-90 is to use the standard capitalization rule when writing the name "Fortran." (Formerly the language name was written with all upper case letters because the name originated as an acronym. It is no longer considered necessary to continue the earlier practice.)

The policy used, thus far, when revising Fortran is this: New versions have been developed that provide greater capability and more features, yet will still run programs written in older versions of Fortran with little or no revisions. This rule still generally holds, since only a few features from FORTRAN-77 have been eliminated in Fortran-90/95/2003. But certain structures have now been marked for planned "obsolescence" and possible elimination in the future (cf. Chapter 19).

Fortran-90/95/2003 is commonly accepted as "standard" Fortran in Europe, but is seen as one of two options in the U.S. (the other being FORTRAN-77). There is yet another version of Fortran, called "High Performance Fortran" or HPF, which is a dialect used on parallel computers (cf. Chapter 19).

Since Fortran has been so widely used for over 50 years, contemporary programmers should know both old and new dialects to be able to read older code as well as make use of the latest features.

1.2 Arranging Program Code

There are currently two different styles used for writing Fortran source (program) code. A general rule prohibits the use of both styles within the same source code file. Different styles may be used in the same program, however, if subprograms of one source form are placed together in one file and compiled independently of subprograms written in the other source form. On many Unix systems, the distinction between styles is indicated by the file extension in this way: When a Fortran-90/95/2003 compiler is available and one is using the newer features of Fortran-90/95/2003, it is best to make use of the newer free source format when entering code into a program file. As a general practice, a programmer should try to keep to one style when writing Fortran-90/95/2003 code, and the newer style is preferred.

Also note that most Fortran compilers are not sensitive to the case of letters of the alphabet. Both upper case and lower case letters may be used and no distinction is made between them. Thus, for example, A and a are considered to be the same letter. Because the card punch machines of the 1950's did not have lower case letters, descriptions and examples in books describing older versions of Fortran are traditionally in UPPER CASE LETTERS ONLY. Books on Fortran-90/95/2003 will usually use a combination of UPPER and lower case letters.

Even though many examples given in this text will use UPPER CASE LETTERS, a programmer should remember that, for most compilers, lower case letters may also be used.

1.3 Free Source Form

Fortran-90 introduced the option to use "free source form" in program code, a form similar to that used in some other languages. In this form, statements can be put anywhere on a line and more than one statement may be included on one line.

"Free source form" is governed by the following norms:

  1. A statement may begin at column 1 or anywhere thereafter and continue to column 132.
  2. Statement labels, if used, may appear anywhere before the statement or structure they label.
  3. Several statements may appear on a single line. If this occurs, the statements are separated by semi-colons. (This is also permitted in the fixed source form of Fortran-90/95/2003, but not in earlier versions of FORTRAN.)
  4. A new line separates one statement from another. Thus, unlike in Pascal or C/C++, a semi-colon is not needed at the end of each line. (Note that statements are therefore not automatically continued from one line to the next without a continuation sign.)
  5. To indicate that a statement continues from one line to the next line, an ampersand (&) is placed at the end of the line to be continued. (There is a different way of indicating that one line continues to the next when using "fixed source form," described below.) One may also include another ampersand as the first character on the next line. (Including both ampersands, the first in column 73 of the first line, and the other in column 6 of the second line, enables the same code to be run in both fixed source form and free source form.) In Fortran-90/95 up to 39 continuation lines are permitted in fixed source form files. In Fortran-2003, up to 255 continuation lines are allowed.
  6. A comment is begun by an exclamation point (in column 1 or elsewhere) and continues to the end of that line. A comment may be included on any line, after program code. Comments exist only for one line. If the text of a comment must continue over more than one line, the comments on each line must be begun anew with a separate exclamation point.
  7. The last line of each program segment must be an END statement which includes the word END optionally followed by the type of the segment and the segment name (e.g., END PROGRAM TEST1).
  8. Blanks are significant and are not ignored and so cannot be used within identifiers. (This is in contrast to the rule for fixed source form, where all blanks, except those in literal strings, are ignored.)

1.4 Fixed Source Form

New versions of Fortran have been so written, that older programs do not (in general) have to be revised and can still be run on new compilers. All versions of Fortran may thus follow rules established in the early 1950's when all input to computers was via Hollerith (so-called "IBM" or "computer punch") cards. This older style for how program code is arranged on lines is now termed "fixed source form." Note that Fortran-90/95 structures and features may be used even when writing code in the older fixed source form!

The following rules apply to program code written in "fixed source form."

  1. There are four types of "lines" (or "cards" if one thinks in terms of entering information via computer cards). ("Cards" and "lines" will be used interchangeably hereafter.)
  2. Each line has 80 columns and lines not used for comments are subdivided into four subsections or "fields."
  3. If the letter C appears in column 1, the entire line is considered to be a COMMENT, and is ignored by the compiler. FORTRAN-77 also allows an asterisk, *, in column 1 instead of a C. A comment line cannot come between an initial line and any continuation lines. A Fortran comment exists only for one line, and cannot be continued to the next line. If the text of a comment must continue over more than one line, each line must be designated as a separate comment line by the use of another C (or *) in column 1.
  4. Fortran-90/95/2003 permits the use of the exclamation point, !, as an alternative starting symbol for a comment line. Fortran-90/95 also permits starting a comment anywhere in the line in fixed source form when beginning the comment with an exclamation point.
  5. Each section of code (i.e. main program, subroutine, function), must have an END statement as the last line physically. An END statement is simply a line which with the word END somewhere in columns 7-72 and blank spaces elsewhere. (Fortran-90/95 free source code also requires an END statement as the last line physically, but this is no longer classified as a distinctively different type of line.)
  6. A line which is neither a continuation line, a comment line, or an END line is called an INITIAL line and can contain any valid Fortran statement (subject to the column restrictions mentioned above).
  7. Older versions of Fortran only allowed one statement per line, but Fortran-90/95 and some versions of FORTRAN-77 permit more than one statement per line using the semi-colon, ;, as a separator.
  8. Some compilers will give an error or warning message if a completely blank line is encountered. Thus, if blank lines are desired to improve readability of the source code, such lines should have a C (or * or !) in column 1 to avoid any unnecessary error or warning messages.
  9. As noted above, blanks are ignored in statements when not included in a literal string. One result is that they can be included within identifiers. Even though blanks are ignored, the position they hold within a line is counted and is used to determine the column 72 limit for statement recognition.

1.5 PROGRAM Header Statement and Program Skeleton

The PROGRAM header statement is an optional initial statement for the main program. Its sole purpose is to associate a name with the main program segment and, from the perspective of programming style, its use is highly recommended.

(The PROGRAM statement was introduced in FORTRAN-77 and its optional character is retained in Fortran-90/95 for the sake of compatibility with earlier Fortran code. Most earlier dialects of Fortran did not recognize the statement.)

If used, its form is:

	PROGRAM name

After the keyword PROGRAM one can put any sequence of letters and numbers (beginning with a letter) forming an identifier to be used as the "name" of the main program.

Compared to other popular programming languages, the Fortran program skeleton is almost non-existent. Because there are no generally "reserved words" in Fortran, the skeleton is merely:

        PROGRAM name          {optional}
       [variable type declarations]  {optional}
       <statements>
        END
(As noted in 1.3, note 7, the last line could also be END PROGRAM or END PROGRAM name.)

1.6 The STOP Statement

In FORTRAN-77, the last executable statement before the END is usually a STOP statement. Its format is merely
	STOP

Fortran-90/95 allow the omission of a STOP if it would come right before the END line.

The STOP statement terminates the program, normally returning control back to the operating system. (Thus, its function is similar to that of a return statement in a C/C++ main() program segment or the exit statement in any C/C++ function.)

Because of the existence of Fortran statements such as IF ... ELSE and various loops (as well as the discouraged "GO TO" statement), the STOP statement (which marks the logical ending point of a program), need not always come right before the END line (which marks the physical ending point of a program segment). It should also be noted that more than one STOP may be included in a program.

To help distinguish between multiple STOP statements, an optional access code may follow the word STOP. This code is either a character string (enclosed by single or double quotes) or a numeric string of up to five digits, for example:

     STOP "Point 1"
     STOP 3245
This access code will appear (sometimes preceded by word STOP, depending on the operating system) when the programs stops and control is returned to the operating system. A STOP statement can be useful when a program code needs an emergency exit in case of data or computational error.

1.7 Sample Program: Free Source Form

The following is a sample Fortran-90/95 code written using the "free source form." The column numbers and line numbers are only for ease of reference and are never inserted in an actual program file.
			Column numbers
		00000000011111111112222222222333333333344444444445
		12345678901234567890123456789012345678901234567890
Line numbers
1		PROGRAM SAMP1
2		!
3		! SAMPLE PROGRAM TO PRODUCE A LIST OF
4		! NUMBERS AND THEIR SQUARES
5		!	
6		! VARIABLE DECLARATIONS
7		!
8		  INTEGER NUM,NUMSQR
9		!
10		  WRITE(6,*) 'Number - Number-Squared'
11		  DO NUM=1,10
12		    NUMSQR = NUM*NUM
13		    WRITE(6,101) NUM,NUMSQR
14		    101 FORMAT(1X,I5,I10)		
15		  END DO
16		  STOP
17		END

COMMENTS

Lines 2-7, and 9 are comment lines. Note that lines 2, 3, 7 and 9 are blank comment lines, inserted for readability.

Lines 11-15 form the calculation loop, which is performed 10 times. The operation of this loop is discussed in Chapter 6. For readability, the statement internal to the loop have been indented.

Lines 10 and 13 are WRITE statements, used to output information from the program. The WRITE statement is discussed in Chapters 3 and 4.

Note that line 14 contains a statement number before the keyword FORMAT. This statement number is referenced back in line 13 (the second number after the keyword WRITE).

Note also that none of the 17 lines in the program is a "continuation" line since in each case, there is no ampersand as the concluding symbol on the preceding line.

Line 16, the STOP statement, is the logical end of the program. After the completion of the loop, there is nothing more to do, so the computer is told to STOP. Since it occurs immediately before the END statement, the STOP statement, in this case, may be omitted.

Line 17, the END statement, is the physical end of this segment of the program. In longer programs, code for subprograms (such as a SUBROUTINE or a FUNCTION) are included at this point, after the main program.

1.8 Sample Program: Fixed Source Form

The following is a sample Fortran-90/95 code using the older "fixed source form." The column numbers and line numbers are only for ease of reference and are never inserted in an actual program file. Remember that statements normally start no earlier than column 7 in this form!
			Column numbers
		00000000011111111112222222222333333333344444444445
		12345678901234567890123456789012345678901234567890
Line number
1		      PROGRAM SAMP1
2		C
3		C     SAMPLE PROGRAM TO PRODUCE A LIST OF
4		C     NUMBERS AND THEIR SQUARES
5		C	
6		C     VARIABLE DECLARATIONS
7		C
8		      INTEGER NUM,NUMSQR
9		C
10	              WRITE(6,*) 'Number - Number-Squared'
11		      DO NUM=1,10
12			  NUMSQR = NUM*NUM
13			  WRITE(6,101) NUM,NUMSQR
14		101       FORMAT(1X,I5,I10)
15		      END DO
16		      STOP
17	              END

COMMENTS

The comments for this section are very similar to the comments for the previous section. One major difference is starting the statement portion of each line in column 7 (or later). Other major differences are noted here.

The comment lines in this example begin with a C in column 1 of each line. In FORTRAN-77, the * could be used in place of a C and in Fortran-90/95, a ! can be used instead.

In the calculation loop in lines 11-15, note that while line 11 and 15 start in column 7, the interior of the loop is indented for readability.

Note that line 14 contains a statement number in the initial field (columns 1-5).

Note also that none of the 17 lines in the program is a "continuation" line since, in each case, column 6 is left blank.

1.9 Good, Bad, and Differences

This section lists the major features of Fortran, which may be useful for those who have programmed in other languages.

Major Positive Points:

Points of Difference between Versions of Fortran and with Other Languages


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1999-2006 Dennis C. Smolarski, S.J., All rights reserved.
Last changed: 5 January 2006.