Notes Fortran 1

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

[Return to Math 60 Homepage | Return to Notes Listing Page]

Contents


Hello World in Fortran [EF 1.1]

C++ Code
        #include<iostream>
	int main()
	{
	   cout << "Hello world!" << endl;
	   return 0;
	}
Fortran 90/95/2003 Code
        PROGRAM TEST1    ! <== this line is actually optional
	   WRITE(*,*) "Hello world!"
	END
Also note similarity with assignment statements:
C++ Code
        a = b + c;
Fortran 90/95/2003 Code
        a = b + c

Introduction to Fortran [EF 1.1]

1955 - IBM - FORmula TRANslation -- San Jose CA
FORTRAN II (late 1950's)
FORTRAN IV - 1966
Univ. of Waterloo, Ontario -- WATFOR
WATFIV (WATerloo Fortran IV)
WATFIV-S (Structured -- Pascal influenced)
FORTRAN 77 (or FORTRAN 5)
Committee hoped to produce a FORTRAN 88, but took too long!
Fortran 90 (introduced new structures and free format style)
Fortran 95 (introduced structures and features for parallel machines)
Fortran 2003 (more features and refinement)
Fortran-90/95/2003 is usually written without all the letters in UPPERCASE.

Statement Types and Format [EF 1.2]

There are currently two different styles used for Fortran source (program) code. In a given program, both styles may be used, but in any one electronic data file, the code should be written either in one style or the other. At SCU, on the HP Math/CS Department server, math, the distinction between styles is indicated by the FILE EXTENSION.

If a file has the extension .f or .for, it is assumed to contain code written in the older FIXED SOURCE FORMAT.

If a file has the extension .f90, it is assumed to consist of code written in the newer FREE SOURCE FORMAT.

Historical Note about Computer Cards

Computer cards = "Hollerith" cards (after Herman Hollerith, the inventor who created the Tabulating Machine Company in 1896, one of the 3 precursors to IBM).

Question: where did size of card come from!?

Answer: see http://www.columbia.edu/acis/history/census-tabultor.html among other sites.

Free Source Code Format [EF 1.3]

For FREE SOURCE CODE FORMAT programs ("new style" or Fortran-90/95/2003 only), there are no restrictions regarding placement of code on a line, except that a line cannot be more than 132 characters long.

Fixed Source Code Format [EF 1.4]

For FIXED SOURCE CODE FORMAT (i.e., "old style") programs, each line has the following restrictions:

(A) If C (or *) in col 1 -- card (line) is a comment -- the rest of the statement is ignored. (One can also use the exclamation point as in Free Source Code Format if one compiles using a Fortran-90/95 compiler.)

(B) OTHERWISE

1-5 used for statement label number (usually blank)
6 used ONLY to indicate this line is a continuation of the previous line (if non-blank is inserted)
7-72 FORTRAN statement
73-80 ignored
IMPORTANT NOTE: In Fixed Source Code format, a common problem is using very long statements (and expressions) and forgetting about the column 72 "limit." ANYTHING which appears past column "72" on a line is ignored, even if it is the middle and end of a variable identifier! Thus it is possible that the end part of long statements will not be read by the compiler.

I do not recommend using Fixed Source Code Format when one has the possibility of using Fortran-90/95/2003 Free Source Code Format.

However, one should know the rules governing Fixed Source Code Format so that one can read older programs and conform to the older restrictions if there is no access to Fortran-90/95/2003. Thus, always use the extension .f90 (or .f95 depending on your compiler).

NOTE: even if one uses the "old" Fixed Source Code format to write a program in a file, one can still use any new Fortran 90/95/2003 statements.

Fortran Skeleton [EF 1.5]

	<statements>
	END
Fortran 90/95 style recommends using an initial PROGRAM statement. This is actually optional for compatibility with earlier versions of FORTRAN.

Its format is

	PROGRAM program_name
I sometimes put the 7+ in examples as a reminder that in older "Fixed Source Code" format, the statement has to begin in column 7 or later.

NOTES:

STOP [EF 1.6]

In FORTRAN-77 (and earlier) STOP is a statement that tells the program that it has reached its LOGICAL end (rather than the physical end of commands). Programs could have more than one STOP statement if there was more than one "terminal" point of the program.

STOP is the last executable statment in the main program.

Usually, it comes, physically, right before the END statement, though not of necessity (depending on the logical flow of the program).

Fortran-90/95/2003 permit omitting the STOP when it would occur immediately before the END.

The function of a STOP is similar to that of a return 0; in C/C++ (for the main function) or exit in C/C++ in any function.

Sample Program: Free Source Form [EF 1.7]

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

Good, Bad, Differences [EF 1.9]

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

Smolarski's Biases

My biases are as follows:

No language is the savior of the computer world, although many languages were considered by many to come close, e.g., PL/I, Algol-68, Pascal, Modula-II, C, Ada. Fortran is not the savior, but it does a lot, and has the advantage of undergoing constant revision and upgrading in such a fashion that older code will continue to run with newer compilers with a minimum of touching up. Fortran-90/95/2003 has features for matrices that are not found in other languages and it will continue to evolve. Even now, with the advent of Fortran-90/95/2003, one is hard pressed to think of something that other scientific languages can do that Fortran would not be able to do.

Peculiarities at SCU on math

If you use Emacs or Xemacs for editing, you can set the editor to highlight in color different language elements differently.

Using Emacs, click on F90 in the top menu bar, and then click on Highlighting and choose the appropropriate degree you wish.

Using Xemacs, click on Options in the top menu bar, then click on Syntax Highlighting, then on Colors. You can increase the degree of highlighting if you wish.

Using Emacs or Xemacs for either Free Source Form or Fixed Source Form, you can use the tab key to adjust the indentation automatically. (For Fixed Source Form, Emacs will skip over the initial label field. In doing this, the first "column" you start typing in is considered to be column 7, even though it may not seem as such.)


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