Fortran 95 - Manual.pdf

(279 KB) Pobierz
Fortran 90/95 Programming Manual
Fortran 90/95
Programming Manual
fifth revision (2005)
Tanja van Mourik
Chemistry Department
University College London
Copyright: Tanja van Mourik
407001587.001.png
Fortran 90/95 Programming Manual
Fortran 90/95 Programming Manual
Brief History of Fortran
The first FORTRAN (which stands for Formula Translation) compiler was developed
in 1957 at IBM. In 1966 the American Standards Association (later the America
National Standards Institute, ANSI) released the first standard version of FORTRAN,
which later became known as FORTRAN 66. The standard aimed at providing a
standardised, portable language, which could easily be transferred from one computer
system to the other. In 1977, a revised version of FORTRAN, FORTRAN 77, was
completed (the standard was published in 1978). The next version is Fortran 90, which is
a major advance over FORTRAN 77. It contains many new features; however, it also
contains all of FORTRAN 77. Thus, a standard-conforming FORTRAN 77 program is
also a standard-conforming Fortran 90 program. Some of FORTRAN 77’s features were
identified as “obsolescent”, but they were not deleted. Obsolescent features are
candidates for deletion in the next standard, and should thus be avoided. The latest
standard is Fortran 95. Fortran 95 contains only minor changes to Fortran 90; however, a
few of the obsolescent features identified in Fortran 90 were deleted.
Because of the requirement that all of FORTRAN 77’s features must be contained in
Fortran 90, there are often several ways to do the same thing, which may lead to
confusion. For example, an integer-type variable can be declared in FORTRAN 77
format:
integer i
or in Fortran 90 format:
integer :: i
In addition, as a legacy from FORTRAN 77, Fortran 90 contains features that are
considered bad or unsafe programming practice, but they are not deleted in order to keep
the language “backward compatible”.
To remedy these problems, a small group of programmers developed the F language.
This is basically a subset of Fortran 90, designed to be highly regular and reliable to use.
It is much more compact than Fortran 90 (because it does not contain all of FORTRAN
77’s features).
There will be a new standard soon, which will be called Fortran 2003 (even though the
final international standard will not come out before late 2004). There will be new
features in Fortran 2003 (such as support for exception handling, object-oriented
programming, and improved interoperability with the C language), but the difference
between Fortran 90/95 and Fortran 2000 will not be as large as that between FORTRAN
77 and Fortran 90.
Introduction to the course
This course intends to teach Fortran 90/95, but from the point of view of the F language.
Thus, many of the old FORTRAN 77 features will not be discussed, and should not be
used in the programs.
1
Fortran 90/95 Programming Manual
It is assumed that you have access to a computer with a Fortran 90 or Fortran 95 compiler.
It is strongly recommended to switch on the compiler flag that warns when the compiler
encounters source code that does not conform to the Fortran 90 standard, and the flag that
shows warning messages. For example:
Silicon Graphics: f90 –ansi –w2 –o executable-name sourcefile.f90
Or even better:
f90 –ansi –fullwarn –o executable-name sourcefile.f90
Sun: f90 –ansi
You can check these flags by typing “man f90” or “man f95” (on a Unix system). You
may ask someone in your group for help how to use the compiler and editor on the
computer you use.
If you have access to emacs or xemacs, and know how to use it (or are willing to invest a
bit of time in learning how to use it), it is recommended to use this editor. It will pay off
(emacs can format the source code for you, and thus detect program mistakes early on).
Bibliography
Fortran 95 Handbook, complete ISO/ANSI Reference
J.C. Adams, W.S. Brainerd, B.T. Smith, J.L. Wagener, The MIT Press, 1997
The F programming language
M. Metcalf and J. Reid, Oxford University Press, 1996
Programming in Fortran 90
I.M. Smith, John Wiley and Sons, 1995
Migrating to Fortran 90
J.F. Kerrigan, O’Reilly & Associates, Inc., 1993
2
Fortran 90/95 Programming Manual
CONTENTS
Chapter 1
Getting started
4
Chapter 2
Types, Variables, Constants, Operators
4
Chapter 3
Control Constructs
15
Chapter 4
Procedures
23
Chapter 5
More on Arrays
35
Chapter 6
Modules
43
Chapter 7
More on I/O
49
Chapter 8
Pointers
55
Chapter 9
Numeric Precision
61
Chapter 10
Scope and Lifetime of Variables
62
Chapter 11
Debugging
65
3
Fortran 90/95 Programming Manual
1. Getting started
Type, compile and run the following program (call the file hello.f90):
program hello
! this programs prints "hello world!" to the screen
implicit none
print * , "Hello world!"
end program hello
Note the following:
a program starts with program program_name
it ends with end program program_name
print * displays data (in this case, the character string “Hello, world!”) on the
screen.
all characters after the exclamation mark (!) (except in a character string) are
ignored by the compiler. It is good programming practice to include comments.
Comments can also start after a statement, for example:
print * , “Hello world!” ! this line prints the message “Hello world!”
Note the indentation. Indentation is essential to keep a program readable.
Additionally, empty lines are allowed and can help to make the program readable.
Fortran 90 allows both upper and lowercase letters (unlike FORTRAN 77, in
which only uppercase was allowed).
2. Types, Variables, Constants, Operators
Names in Fortran 90
A name or “identifier” in Fortran must adhere to fixed rules. They cannot be longer than
31 characters, must be composed of alphanumeric characters (all the letters of the
alphabet, and the digits 0 to 9) and underscores ( _ ), and the first character must be a
letter. Identifiers are case-insensitive (except in character strings like “Hello world!” in
the example above); Thus, PRINT and print are completely identical.
Types
A variable is a data object whose value can be defined and redefined (in contrast to
constants , see below). Variables and constants have a type , which can be one of the five
intrinsic types , or a derived type . Intrinsic types are part of the Fortran language. There
are five different intrinsic types. In Fortran 90, there is additionally the possibility of
defining derived types, which are defined by the user (see below). The five intrinsic
types are:
Integer Type
For integer values (like 1, 2, 3, 0, -1, -2, …).
4
 
Zgłoś jeśli naruszono regulamin