[General Rules]   [A Few Basics Statements]   [Example Program]   [Fortran 90 Features]   [Summary]   [Problems]     

CHAPTER 1:  OVERVIEW



The Fortran computer language remains popular among scientists and engineers, who use it to develop computer programs and other software applications in their respective fields. The terms processor and compiler are used in this book in reference to the execution of computer programs.

A processor consists of a computer system and the mechanism(s) by which computer programs are transformed for use in that system. A compiler is a specific commercial version of a computer language. The purpose of the compiler is to translate a set of English-like instructions, written in a computer language such as Fortran, C, Basic, Pascal, or Cobol, into a set of machine-language instructions, which are understood by the processor. A programmer is a person that writes, tests, and runs a computer program to solve a certain task or problem.

Several Fortran compilers are in current use. These compilers adhere to national standards (ANSI X3.9-1978, informally referred to as FORTRAN 77), or alternatively, they constitute a subset or superset of them. This book deals primarily with standard FORTRAN 77. Where appropriate, extensions to the standard are indicated. At the end of each chapter, selected features of the new national standard (ANSI X3.198-199x, informally referred to as Fortran 90) are introduced.

Conversion to the new standard should continue throughout the remainder of the decade, as new compilers are developed to suit a variety of processors.


1.1  GENERAL RULES

[A Few Basics Statements]   [Example Program]   [Fortran 90 Features]   [Summary]   [Problems]      [Top]  

Statements

The basic unit of the Fortran language is the statement keyword, which has a specific meaning to the processor. Table 1.1 shows a comprehensive list of Fortran statement keywords. In common usage, statement keywords are re ferred to simply as statements, or also, keywords.

A Fortran statement consists of a Fortran keyword, either by itself or as part of a complete sentence subject to rules of syntax. (This is the grammar of the Fortran language). However, certain statements, for instance, the arithmetic assignment statement (Chapter 5), may have no Fortran keyword. A valid Fortran statement has the correct spelling and syntax. Syntax rules are explained throughout this book.

Fortran statements can be either: (1) executable, or (2) nonexecutable. Executable statements describe specific actions to be carried out by the processor. Nonexecutable statements help configure the environment in which these actions take place. Table 1.1 lists executable and nonexecutable Fortran statement keywords.

TABLE 1.1  FORTRAN STATEMENT KEYWORDS

Executable Nonexecutable
ASSIGN BLOCK DATA
BACKSPACE CHARACTER
CALL COMMON
CONTINUE COMPLEX
CLOSE DATA *
DO DIMENSION
DO WHILE DOUBLE PRECISION
ELSE EQUIVALENCE
ELSE IF EXTERNAL
END FORMAT *
END DOFUNCTION
END IF IMPLICIT
GOTOINTEGER
IF(exp)THENINTRINSIC
OPENLOGICAL
PAUSE PARAMETER
PRINT PROGRAM
READ REAL
RETURN SUBROUTINE
REWIND
STOP
WRITE

* DATA and FORMAT statements can span the range
of executable and nonexecutable statements (see Fig. 1.1).

Program Units

A program unit is a sequence of statements defining a certain computing procedure, meaningful to the programmer. There are four types of program units:

1.  Main program.

2.   Function subprogram.

3.   Subroutine subprogram.

4.   Block data subprogram.

The main program (required) is the principal program unit. The subprograms (optional) are secondary program units.

An executable program consists of one or more program units, including one (1) main program and zero (0) or more subprograms. Within one executable program, the program units may be placed in any sequential order. It is good programming practice to place the main program at the top, followed by the subprograms, if any.

The first and last statement of every program unit are fixed, as follows:

  • The first statement of a main program is PROGRAM.

  • The first statement of a function subprogram is FUNCTION.

  • The first statement of a subroutine subprogram is SUBROUTINE.

  • The first statement of a block data subprogram is BLOCKDATA.

  • The last statement of every program unit, regardless of type, is END.

An executable program consists of the Fortran source program and optional comments, which are not part of the source program. Comments are used to explain the con tents of a source program. Comments are not statements and are not intended to affect source program processing in any way.

Comments can be incorporated into a source program in the following two ways:

  1. By placing the letter C, an asterisk (* in Times font), or an exclamation point (!) in the first column of any line. This signals the processor to treat the remainder of the line as a comment line. Lines containing only blank spaces (no characters) are also treated as comment lines, and may be used freely to improve readability.

  2. By placing an exclamation point (!) in any other column (2-80), excluding column 6. This signals the processor to treat the remainder of the line as a trailing comment.

The example below shows several comment lines. Note that the top two lines show the position of the first fifty columns of type. This is a good way of identifying the column numbers, in case it is not provided by the text editor. (The text editor is the software used by the program mer to physically input, i.e., write, the source program onto memory). This or a similar notation will be used throughout the text. The COURIER typeface indicates what should be input onto the screen. The Helvetica typeface is used here (and in the remainder of this chapter) for explanation.

Example: Comment Lines

C--------1---------2---------3---------4---------5
C2345678901234567890123456789012345678901234567890

Valid comment lines using C in column 1:

C-----THIS PROGRAM WAS WRITTEN BY R. L. JONES.
C-----VERSION 2.5, OCTOBER 1994.

Valid comment lines without using C in column 1:

***ASSIGNMENT NUMBER 5 FOR CSC 220
!BEGINNING OF MAIN LOOP.

Valid trailing comments:

      C= SRQT(A**2 + B**2) !CALCULATION OF C
      Z= X + Y !CALCULATION OF Z

Compiling, Linking, and Running Programs

An executable program needs to be compiled (translated into machine language) and linked (prepared for execution by the processor) before it can be run (the instructions executed) and results obtained and analyzed. Some compilers will have separate Compile and Link commands, while others will have only one command, to be used to compile and/or link. How to run a Fortran computer program will depend on the operating system of each computer.

A program being developed (written for the first time, or modified to suit new applications) may contain any number and type of errors. Debugging is the process of finding and getting rid of these errors. Errors during compilation, linking, or execution will usually trigger onscreen error messages to assist the programmer in diagnosis.

Order of Statements

Within a program unit, Fortran statements follow a certain order or sequence. Violating this order will cause error messages to be produced during compilation.

Figure 1.1 shows the required order of Fortran state ments comprising a source program. Vertical lines separate statements that can be interspersed (mixed sequence). Horizontal lines separate statements that cannot be interspersed. For example:

  • The PROGRAM statement precedes all other statements in a program unit.

  • A PARAMETER statement precedes all executable statements in a program unit.

  • A DATA statement can be interspersed throughout most of the program unit, but must precede the END state ment.

  • A Comment line can be interspersed throughout the program unit, including PROGRAM and END statements.

Usage of these statements will be discussed later. Figure 1.1 should be used for future reference.

Comment
Lines
PROGRAM     FUNCTION    SUBROUTINE    BLOCK DATA
FORMATIMPLICITPARAMETER
DATAOther Nonexecutable
Statements
Statement Function Definitions
Executable Statements
END

Fig. 1.1 Order of Statements Comprising a Source Program.

Character Set

FORTRAN has the following character set:

  1. All uppercase letters (A through Z). Certain processors may permit lower-case letters (a through z).

  2. The ten digits (0 through 9).

  3. The underscore _, which may be used as a significant character in a name.

  4. A set of special characters, depending on the processor. A typical set is: equals =, plus +, minus -, asterisk *, slash /, left parenthesis (, right parenthesis ), comma ,, decimal point or period ., apostrophe ', colon :, exclamation point !, quotation mark or quote "", percent %, ampersand &, semicolon ;, less than <, greater than >, question mark ?, and currency symbol $.

Lines and Fields

A Fortran line consists of eighty (80) columns separated into four fields:

  1. Label field: Columns 1-5.

  2. Continuation field or column: Column 6.

  3. Statement field: Columns 7-72.

  4. Sequence number field: Columns 73-80.

Their spatial relationship is shown below.

LABEL FIELD 1-5                    STATEMENT FIELD 7-72

         1         2         3         4         5         6         7         8
12345678901234567890123456789012345678901234567890123456789012345678901234567890

CONTINUATION COLUMN 6                                SEQUENCE NUMBER FIELD 73-80

The label field can accomodate a statement label consisting of at least 1 and at most 5 numerals, for example: 220, or 12379. The following are syntax rules for label fields and their respective statement labels:

  • Blank spaces and leading zeroes are ignored within a label field.

  • An all-zero statement label is invalid.

  • Any Fortran statement can have a label. However, only those statements that are referred to by other statements require a label.

  • It is good programming practice to label only those statements that need labels.

Placing the letter C, an asterisk *, or an exclamation point ! in the first column of a label field is taken to indicate a comment line. Some processors permit the use of a D in column 1, to indicate a debug line. In the following example, the first line is an abbreviated form of the column number guide.

Example: Label field

C234567890

Valid statement label (within columns 1-5):

220   C= A + B

Valid statement label with leading blank space:

 320  C= A + B

Valid statement label occupying all five columns (1-5):

12500 R= P - Q

Invalid statement label (one or more zeros within columns 1-5):

0     Z= X/Y

A valid debug line (D in column 1):

D     PRINT *, C,R,Z

Debugging and D Lines

  • Debugging refers to the process of getting rid of errors in statement/keyword spelling or syntax.

  • D Lines have a D in Column 1.

  • Note that during normal source program compilation, the D lines are treated as C lines (comment lines).

  • During compilation using the D_LINES qualifier (e.g., FORTRAN/D_LINES), the D lines are compiled as part of the source program.

  • The D_LINES feature allows you to turn on or off selected statements during the debugging phase of program development.

The continuation field (Column 6) is used to accomodate a statement that is too long to fit within one line. Syntax rules follow:

  • Any valid character, other than zero (0) or a blank space, placed in Column 6 will indicate that the respective statement field (Columns 7-72) is a continuation of the previous source line.

  • The maximum number of successive continuation lines is processor-dependent. A typical value is 19 succes sive continuation lines.

  • Comment lines cannot be continued.

The statement field comprises Columns 7-72 and it contains the statement/keyword proper. Syntax rules follow:

  • Within the statement field, blank spaces are ignored by the compiler. Blank spaces are used to improve the readability of the source program.

  • The space character (or blank space) is ignored within the statement field, except when it appears within a character constant. For example, the forms ELSEIF and ELSE IF are equivalent, as are the forms Z=X+Y and Z = X + Y. On the other hand, the forms 'CHAR' and 'CHA R' are not equivalent.

Example: Continuation column

         1         2         3         4         5         6         7         8
C2345678901234567890123456789012345678901234567890123456789012345678901234567890

      READ(5,100) A1,B1,C1,D1,E1,F1,G1,H1,O1,P1,Q1,R1,S1,T1,U1,V1,W1,X1,
     1Y1,Z1

The 1 placed in column 6 of the second line indicates that this line is a continuation of the prior source line.


Example: Statement field

         1         2         3         4         5         6         7         8
12345678901234567890123456789012345678901234567890123456789012345678901234567890

Valid statement because it starts in column 7:

      READ(5,100) A1,B1,C1

Invalid statement because it starts in column 6:

     READ(5,100) A1,B1,C1

Invalid statement because it extends beyond column 72:

      READ(5,100) A1,B1,C1,D1,E1,F1,G1,H1,O1,P1,Q1,R1,S1,T1,U1,V1,W1,X1,Y1,Z1

Things to watch for with regard to statement fields:

  • Do not type statement field information in Columns 1-6. It will trigger a compiler error message.

  • Do not type statement field information in Columns 73-80. A common source of error is to run the statement field over to column 73 and beyond.

  • Within Columns 7-72, use blank spaces freely to make your program easier to read.

The sequence number field comprises Columns 73-80. This field was necessary in the early Fortran standard when input was performed through punch cards. Now source programs and data are stored in magnetic media (floppy diskettes or hard disks), largely eliminating the need for a sequence number field. The compiler ignores the characters in this field. However, comments lines or trailing comments may extend beyond Column 72, to Column 80 if necessary


1.2  A FEW BASIC STATEMENTS

[Example Program]   [Fortran 90 Features]   [Summary]   [Problems]      [Top]   [General Rules]  

PROGRAM Statement

The PROGRAM statement is the first statement of a main program unit. It is an optional statement. However, it is good programming practice to use it to distinctly specify the name of the main program. It takes the form:

PROGRAM MYNAME

where MYNAME is the name given to the main program. In this case, the compiler refers to the main program with the name of MYNAME.

The following guidelines regarding PROGRAM state ments should be adhered to:

  • The program name should be distinct and readily recognizable by the user.

  • The maximum allowable length of the program name is 31 valid characters (processor-dependent).

  • The program name should not be used to name any other program unit, common block labels (Chapter 9), or main program variable names, within the same executable program. This will trigger a compiler error message.

  • The underscore _ can be used for added clarity in naming programs consisting of more than one English word.

  • In the absence of a program statement, the processor as signs the generic name MAIN to the main program.

Example: PROGRAM statement

C234567890
      PROGRAM MATRIX

END Statement

The END statement marks the end of a program unit. It must be the last source line of every program unit (main program or subprogram).

The END statement takes the form:

END

If the END statement is reached in a main program, an implicit STOP terminates program execution. If the END statement is reached in a subprogram, an implicit RETURN is executed (Chapter 9), with control returning to the invoking program or subprogram.

PRINT Statement

The PRINT statement, an executable statement, is used to transfer data from the computer's internal storage to the default output device, which is usually the screen. It is the simplest (and most limited) way to output (write out) data.

It takes the form:

PRINT *, A,B,C

where A, B, and C are variables whose values are to be printed. The asterisk indicates a free-format, wherein the output format is controlled by the processor. The comma is used to separate the asterisk and variable names.

Alternatively, the PRINT statement takes the form:

PRINT *, 'THE VALUES ARE ',A,B,C

where the character constant THE VALUES ARE (en closed within apostrophes in the statement) now precedes the numeric output (A, B, C).

The following short program illustrates the usage of the PRINT statement.

Example Program: Use of PRINT statement

C234567890
      PROGRAM ADDITION
      X= 3.
      Y= 4.
      Z= X + Y
      PRINT *, 'THE VALUE OF Z IS = ', Z
      END

When compiled and run, the above program will produce the following output on the screen:

THE VALUE OF Z IS = 7.0000

The PRINT statement can also be used to print out a character constant, as in the following example:

PRINT *, 'MESSAGE NUMBER 3'

Any alphanumeric constant contained within the apos trophes, including leading, embedded, and/or trailing blank spaces, will be printed.

Example Program: Use of PRINT statement

C234567890
      PROGRAM PRINT_LITERAL
      PRINT *, '    D '
      PRINT *, '   D D '
      PRINT *, '  D   D '
      PRINT *, ' D     D '
      PRINT *, 'D D D D D'
      END

The following output will be produced on the screen:

    D
   D D
  D   D
 D     D
D D D D D

STOP Statement

The STOP statement is an executable statement, placed anywhere within a program unit to stop execution, when so desired by the programmer. It takes the form:

STOP display

A program unit may have zero or more STOP state ments. The argument display is optional; it may be used to show on the screen which STOP statement triggered program termination. The argument can be either a character constant or an integer constant of up to 6 digits. If display is omitted, the following message may be shown on the screen:

FORTRAN STOP

Example: STOP Statement

C234567890
C-----EXAMPLE 1: STOP WITH CHARACTER DISPLAY
      STOP 'EUREKA'

C-----EXAMPLE 2: STOP WITH NUMERIC DISPLAY
      STOP 10

In Example 1 above, the following message is displayed on the screen:

EUREKA

In Example 2 above, the following message is displayed on the screen:

10

If an executable program has no STOP statement, an implicit STOP is executed when control reaches the end of the main program. In this case, no message will be displayed on the screen.


1.3  EXAMPLE PROGRAM

[Fortran 90 Features]   [Summary]   [Problems]      [Top]   [General Rules]   [A Few Basics Statements]  

The following is an example program that accomplishes the following steps:

  1. Defines two variables (A and B) by using assignment statements (Chapter 5).

  2. Proceeds to do a sum (A+B), subtraction (A-B), multiplication (A*B), division (A/B), and exponentiation (AB) to generate the values of variables V, W, X, Y, and Z, respectively.

  3. The results are printed to the screen, with each value preceded by an appropriate label.

Example Program

C234567890
C-----EXAMPLE OF A COMPLETE PROGRAM UNIT
      PROGRAM OPERATIONS
      A= 16.23
      B= 2.4
      V= A + B
      W= A - B
      X= A*B
      Y= A/B
      Z= A**B
      PRINT *, 'A + B = ',V
      PRINT *, 'A - B = ',W
      PRINT *, 'A * B = ',X
      PRINT *, 'A / B = ',Y
      PRINT *, 'A TO THE POWER B = ',Z
      END

The output would be produced as follows:

A + B = 18.63000

A - B = 13.83000

A * B = 38.95200

A / B = 6.76250

A TO THE POWER B = 803.09042


1.4  FORTRAN 90 FEATURES

[Summary]   [Problems]      [Top]   [General Rules]   [A Few Basics Statements]   [Example Program]  

Overview

The new standard Fortran 90 is a superset of FORTRAN 77, which means that, subject to a few limitations, FORTRAN 77 is contained entirely within Fortran 90. Therefore, a program that follows FORTRAN 77 standards also follows Fortran 90.

While not a single feature of FORTRAN 77 has been deleted in Fortran 90, a few have been categorized as obsolescent. Fortran 90 recommends that the next Fortran standard (year 2000+) consider for deletion only the language features that appear in its list of obsolescent features.

Source Form

The source form refers to the arrangement of lines and fields in a source program. Fortran 90 permits two source forms: free and fixed. The way of specifying the source form of a program unit is a processor-dependent feature. Free form and fixed form must not be mixed in the same program unit.

Fixed Source Form

The fixed source form of Fortran 90 is essentially that of FORTRAN 77 (see Section 1.1), with the enhancement of the semicolon (;), used as a statement separator in a single source line (except when it appears within a character variable or as part of a comment).

Free Source Form

Fortran 90 allows a free source form. In free source form, each source line may contain up to 132 characters, and there are no restrictions on where a statement (or a portion of a statement) may appear within a line. In other words, a source line may begin in the first column, rather than in column 7, as with Fortran 77.

Other significant features of the Fortran 90 free source form are:

  • Blank spaces are meaningful in a source line. A sequence of blank spaces (except when it appears within a character variable) is equivalent to a single blank space.

  • A blank space must be used to separate statement labels, names, and constants, from adjacent statement labels, names, or constants. For example, in

    30 Z= X + Y

    the blank is required between statement label 30 and variable Z.

  • Embedded blank spaces are optional in most statement keywords (see Table 1.1). However, they are required in some cases, notably in the DO WHILE statement (Section 7.2) and to separate the function keyword from its type declaration, as in INTEGER FUNCTION (Section 9.2).

  • The exclamation point (!) initiates a comment (except when it appears within a character variable). The semicolon (;) separates statements on a single source line (except when it appears within a character variable). A semicolon appearing as the last nonblank character of a source line (ahead of commentary, if any) is ignored.

  • The ampersand & is used to indicate that the current statement is continued on the next line and that it is not a comment line. When used for continuation, the ampersand is not part of the statement. A free form statement is limited to 39 continuation lines.

  • Comment lines cannot be continued. An & in a comment line has no effect.

In free source form, the example program of Section 1.3 can be written as follows:

Example: Fortran 90 free source form

C234567890
PROGRAM OPERATIONS
A= 16.23
B= 2.4
V= A + B
W= A - B
X= A*B
Y= A/B
Z= A**B
PRINT *, 'A + B = ',V
PRINT *, 'A - B = ',W
PRINT *, 'A * B = ',X
PRINT *, 'A / B = ',Y
PRINT *, 'A TO THE POWER B = ',Z
END
!END OF PROGRAM OPERATIONS


1.5 SUMMARY

[Problems]      [Top]   [General Rules]   [A Few Basics Statements]   [Example Program]   [Fortran 90 Features]  

  • This chapter introduces general Fortran rules and a few basic statements.

  • Statements are clasified as either executable or nonexecutable.

  • Four types of program units are defined: (1) main program, (2) function subprogram, (3) subroutine subprogram, and (4) block data subprogram.

  • Comment lines (C, *, or ! in the first column) are used to explain the contents of the source program. Comments are not statements and are not intended to affect source program processing in any way.

  • Four fields are introduced: label field (1-5), continuation column (6), statement field (7-72), and sequence number field (73-80).

  • The following basic statements are introduced and explained: PROGRAM, END, PRINT, and STOP.


PROBLEMS

   [Top]   [General Rules]   [A Few Basics Statements]   [Example Program]   [Fortran 90 Features]   [Summary]  

  1. Write a program to print your name, address, social security number, and date of birth using character constants.

  2. Write a program to assign the values 3., 4., and 5. to variables A, B, and C, respectively; to sum all three values and to print the answer. Write the following message preceding the output: THE SUM OF ALL THREE VALUES IS EQUAL TO .

  3. Write a program to calculate and print in free format the value X= 25. Use 2 followed by a period (as in 2.). The period after 5 is optional (More about this in Chapter 3). Use the following label preceding the output: THE VALUE OF 2 TO THE POWER 5 IS= . Note the number of significant figures that the free format output allows in your computer.

  4. Write a program to print the following design of the letter W:

    W         W         W
     W       W W       W
      W     W   W     W
       W   W     W   W
        W W       W W
         W         W

  5. Write a program to calculate and print in free format the value Y= 2.518.4. Note the number of significant figures that the free-format output allows in your computer. Compare with the answer given by your hand calculator. To what do you attribute the difference, if any?

  6. Repeat Problem 5 adding a labeled STOP. Label the stop: GOOD WORK! Observe what happens when you run the program.

  7. Write a program to print the following numeral pattern:

    0123456789
    1234567890
    2345678901
    3456789012
    4567890123
    5678901234
    6789012345
    7890123456
    8901234567
    9012345678

  8. Using your screen editor to cut and paste repetitive character strings, modify Problem 7 so that the numeral pattern is repeated five times across the page, each separated by three spaces. [Hint: Print the output line by line, and use the continuation field if necessary].

  9. If your computer is a 32-bit computer, write a program to calculate and print I= 230 (Do not follow 2 with a period in this case). Then, modify your program and attempt to calculate I= 232. What happens to the modified version? What can you surmise about the capabilities of your computer? If your computer is a 16-bit computer, use I= 214 and I= 216 instead.

  10. Computers store different types of numbers in different ways. For instance, take the values 3 and 3.5; the first is an integer constant, and the second is a real constant. Write a program that reverses this by assign ing R= 3, and I= 3.5. Print the values of R and I (each preceded by an appropriate label) and observe what happens. What can you surmise from the results?

  11. Modify Problem 10 to assign I= 3 and R= 3.5. Observe what happens when the program runs. What can you surmise from this exercise?

  12. Write a program to print a banner showing your initials across the page. Use the W of Problem 4 as an example.

  13. Write a program to calculate and print the weight of an object, given its VOLUME= 15 cubic feet, and DENSITY= 40 lbs. per cubic foot. The output should read: THE WEIGHT OF THE MYSTERY OBJECT HAS BEEN CALCULATED TO BE 600 LBS. Note that under free format, the actual form of the numeric output is controlled of the processor.

  14. Write a program to print the QWERTY sequence of your keyboard, using three cases, each case separately: (1) no-shift, (2) shift, and (3) lock keys. Separate each character with a blank space, and each case with a blank line [Hint: Use PRINT * only. To print a single apostrophe in a character context, place two consecutive apostrophes with no blank spaces in between them].


http://fortran.sdsu.edu 140611 14:00

Documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view; download Adobe Acrobat Reader.

Open-channel hydraulics, Victor M. Ponce, San Diego State University, Copyright © 2014, All rights reserved.


FORTRAN FOR SCIENTISTS AND ENGINEERS

VICTOR M. PONCE

•  ONLINE EDITION  •

Contents

Chapter 01

Chapter 02

Chapter 03

Chapter 04

Chapter 05

Chapter 06

Chapter 07

Chapter 08

Chapter 09

Chapter 10

Chapter 11

Copyright © 2014 • Victor M. Ponce • All rights reserved.