Notes Fortran 1
[Return to Math 60 Homepage |
Return to Notes Listing Page]
Contents
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
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.
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.
-
One style is called Fixed Source Format and
it is the older style in use before Fortran-90/95/2003. It is based on the
limitations of the 80 column punch card. Thus, EACH COLUMN IN EACH
LINE IS IMPORTANT!!!
-
The other style is called Free Source Format and is the style
commonly used in other contemporary languages, in which there is
no limitation as to where code can be placed on a line.
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.
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.
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.
- A statement can begin at column 1 or anywhere else, and continue up to
column 132.
- Several statements are permitted on 1 line.
If you include more than one
statement, they must be separated by a semicolon.
This is also permitted in Fixed Source Format.
- A semicolon is not needed at the END of a line, since the
newline also separate statements.
- IN FREE SOURCE FORMAT, the continuation is indicated by including
the ampersand, &, at the end of the line to be continued.
- IN FREE SOURCE FORMAT, a comment is begun by an exclamation point
and continues until the end of the line. A comment may be included
on any line after any statements on that line. This is also
permitted in Fixed Source Format.
- IN FREE SOURCE FORMAT, blanks are significant.
(They are still not significant in Fixed Source Format.)
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.
<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:
- Completely blank lines CAN cause problems in some compilers.
- Use blank COMMENT Cards for spacing instead.
- Common Fortran style recommends putting only one statement on a line.
(This was demanded by older versions of FORTRAN.)
In Fortran-90/95, more than one statement can be on a line
separated by a semi-colon, as in C/C++, even though this is
not recommended.
- In Fortran-90/95, you can insert a
COMMENT after a statement by using a !
Everything after the ! until the end of the line is
considered a comment.
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.
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
This section lists the major features of Fortran, which may be
useful for those who have programmed in other languages.
Major Positive Points:
- PORTABLE CODE: Because of the standardization of Fortran,
programs can be readily moved from one compiler to another, and
usually run with only minor changes, if any.
- EFFICIENT COMPILERS: Fortran compilers are very well
developed and efficient, often with many optimization features, and are
available for almost all machines, from personal computers to
parallel computers. Because of compiler efficiency, Fortran programs
run very fast and usually out-perform compilers for most other
languages.
- EXPONENTIATION OPERATOR: Unlike many other languages,
Fortran has an exponentiation
operator, **, which makes it particularly useful for solving
numerical problems.
- MATHEMATICAL FEATURES: Standard Fortran contains several
features which make it very appropriate for mathematical
programs: complex and multiple-precision real data types, the
exponentiation operator, and a large library of built-in mathematical
functions.
- POWERFUL INPUT/OUTPUT: Fortran has a developed input/output
system with many features. As a result, the input and output
system is very powerful (but can also be very confusing to use).
Points of Difference between Versions of Fortran and with Other
Languages
- BLANKS ARE IGNORED IN FIXED SOURCE FORM: As noted earlier,
all blanks (except in literal quoted strings) are ignored
when using Fixed Source Form. Thus this style of source code
(used in FORTRAN-77 and other earlier versions) differs from some
other languages, where certain blanks are needed, and only "extra"
blanks and carriage returns are ignored. In fixed source form,
blanks are even ignored as part of variable names (see
Chapter 2).
- IN FORTRAN-77, IDENTIFIERS MAY BE SIX CHARACTERS LONG AT MAXIMUM:
In FORTRAN-77 (and other earlier versions of FORTRAN), variables and
subprogram names could (normally) be at most six characters long.
Therefore, a programmer had to be somewhat creative in
choosing appropriate identifiers. In Fortran-90/95, this limitation
has been extended.
- DEFAULT VARIABLE TYPING: Variables do not have to be declared
in Fortran. Variables that are not explicitly declared (some due to
a typographical error) are given a default numeric `type' (either integer or
real) based on the first letter (cf.
section 2.2). One can turn
"off" default typing by including the statement IMPLICIT NONE before
any variables are declared.
- NO RECURSION PRIOR TO FORTRAN-90/95: FORTRAN-77 subprograms are
not recursive, but Fortran-90/95 has introduced recursion for subprograms.
- ALL VARIABLES ARE LOCAL: All variables in Fortran are local to
the subsection in which they are used and/or declared. There are
no global variables in Fortran. On the other hand, the old FORTRAN
COMMON statement and the Fortran-90/95 MODULE construction
are ways to designate certain variable as being able to be shared by
multiple segments of a program.
- STATEMENT NUMBERS ARE LOCAL: All
statement numbers (if used) are local. Numbers used in one
subsection are independent of any used in another subsection.
- NO EXPLICIT DESIGNATION OF BLOCKS: Unlike C/C++ or other languages,
Fortran needs no special set of delimiters to indicate a block
of statements, e.g., the { ... } pair in C/C++ (or
the begin ... end pair in Pascal). Most Fortran-90/95 statements or
constructs assume that there will be more than one statement in
the body of the construct. Thus only a labeled END (e.g., END IF
or END DO) is used to indicate the end of the construct.
Often, there is only one END per section--physically
the last line of the section, typically unlabeled.
- SELECTED USE OF SEMI-COLONS: Standard Fortran does not use
semi-colons to end or separate statements appearing on
different lines. (Prior to FORTRAN-77, only one statement
could appear on a line.) If the programmer desires to put more
than one statement on a line, the statements are separated by
semicolons.
- NEW LINES HAVE MEANING: Because semi-colons are not part of
the normal Fortran syntax, the new line (carriage return) usually
is the statement separator. If one line continues information
from the previous line (e.g., a very long arithmetic expression),
it must be indicated as a continuation line.
As noted
above, this is done in Fixed Source Form by using a non-blank
character in column 6 of the line that continues
the previous line, or, in Free Source Form, by using an
ampersand as the last character of the line to be continued.
- SPAGHETTI-CODE SYNDROME: Old versions of FORTRAN could lead
to a so-called "spaghetti-code" style of programming, in which
later sections of the code could jump to earlier sections and
earlier sections to later sections. This syndrome could be avoided, however,
if the programmer used disciplined, structured techniques. The
"spaghetti-code" style means that older FORTRAN
programs are often much harder to read than programs written in
FORTRAN-77, Fortran-90/95 or other "structured" languages.
My biases are as follows:
- Fortran is not standardized completely; it can lead to
messy code (especially in the older programming style); its compilers
can be nightmares for developers; there are many local variants.
-
BUT Fortran is here to stay. Some of the "supercomputers" have
only two compilers available: C and Fortran, and the Fortran compiler
is better optimized than the C compiler (i.e., runs codes faster
because it is more efficient).
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.
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.