Fortran 95
eBook - ePub

Fortran 95

M Counihan

Share book
  1. 418 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Fortran 95

M Counihan

Book details
Book preview
Table of contents
Citations

About This Book

This is the second edition of the first introductory textbook written for the FORTRAN 90 standard. It remains suitable for the novice scientific programmer, drawing on a larger number of examples and exercises in this new edition.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Fortran 95 an online PDF/ePUB?
Yes, you can access Fortran 95 by M Counihan in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Desarrollo de software. We have over one million books available in our catalogue for you to explore.

Information

Publisher
CRC Press
Year
2006
ISBN
9781135366049

CHAPTER 1
What is Fortran 95?

This introductory chapter describes the evolution of the Fortran programming language and its new extensions. The nature and structure of a Fortran program, its division into separate program units of different types, the existence of intrinsic procedures, the distinction between declarative and executable statements, and the types of data that can be represented in the language, are introduced. Some comments are made about coding conventions.

1.1
The Fortran language family

Fortran is a world-standard computer programming language approved under the auspices of the International Standards Organization (ISO). It is the leading language for scientific computing and it is an evolving language that has existed for over 40 years in various forms. Fortran 90 replaced FORTRAN 77 a few years ago, and in turn is being replaced now by the latest version, Fortran 95. Fortran’s evolution has been the key to its longevity: it has developed in response to progress in computer architecture and to new insights in programming language theory, it has been used for solving an increasingly broad range of problems, and it will certainly continue to develop in the future to enable computer users to take full advantage of innovations to come. Fortran 90 was accepted as an international standard by ISO in 1991 and it was a major expansion of the earlier FORTRAN 77. At the time of writing,
Fortran 95 is expected to gain final ISO approval in mid-1996. Fortran 95 will be a relatively minor revision of Fortran 90 and most programs written in. Fortran 95 will appear very similar to those in Fortran 90. This book can be used equally well for Fortran 90 as for Fortran 95. The next major revision of Fortran, expected to take place very early in the next century, is already being contemplated and is termed “Fortran 2000” or “f2k”.
Although Fortran includes an impressive set of mathematical functions, and is the most advanced highlevel programming language for scientific and technical computing, it is not intended only for mathematical work. In fact, Fortran handles text and general datasets with the same flexibility that it handles numbers. So, while containing numerous mathematical examples, this book is intended to be equally valuable to nonmathematicians who will be using Fortran to solve problems of other kinds.
Fortran involves ideas and terminology which have become standard in the world of software engineering, with concepts like “character” and “pointer” and “argument” that are also used in describing other programming languages. These terms have precise technical meanings and are part of the vocabulary that program-writers need to discuss their work. Beginners should try to get used to the terminology even when it sounds rather pompously long-winded, e.g. “structure constructor”, and “relational intrinsic operator”. This book deliberately uses accurate technical terms as often as possible, since serious Fortran users will need to become familiar with the terminology as well as learning the keywords and syntax of the language itself.
Uniquely among computer programming languages, the evolution of Fortran is self-consciously progressive and open-ended, and arrangements are in place for the steady development of the language beyond Fortran 95. It is intended that the language should be expanded in a modular fashion, extending but not modifying what is already in existence. A module subprogram, which encapsulates new procedures and derived data types in a self-contained package, is a natural way of implementing such extensions to Fortran. The first such module to gain ISO approval, ISO_VARYING_STRINGS, is described in detail in Appendix C.
“High Performance Fortran”, or HPF, is a much more elaborate extension to Fortran than ISO_VARYING_STRINGS. HPF has arisen because new kinds of computer architecture demand to be exploited as efficiently as possible. HPF is for programmers who need to push against the performance envelope of the most advanced parallel-processing systems that exist today.

1.2
Programs and programming

A computer program is simply a sequence of instructions to be carried out by a computer. A computer is a composite mechanical system that works by the transmission of signals and data between components such as keyboards, optical and magnetic disks, semiconductor chips with micron-scale circuits for storing and processing data, visual display screens and printers. A computer system also includes special programs— system software—whose task is to manage the flow of information around the system itself. “Operating systems” are a species of system software. Some computer systems have sophisticated interfaces with telecommunications systems that connect them with other computers, including the global network of computers known as the Internet, and recent years have seen the evolution of “open systems” in that the boundaries between one computer system and others have become blurred.
But what are computers for? Ultimately, computers exist in order to exercise control over equipment external to the computer, or to make calculations and to solve problems that lie outside computer science. A computer might control a production line, or it might predict the likely result of a presidential election, or the likely course of a tornado. To do such jobs, special-purpose programs must be written. Because a computer is a general-purpose tool, it cannot be expected that a special-purpose program will always be installed on the computer for which it was originally written: it may happen that the program will be used on many different computers, perhaps manufactured by different companies and running under the control of different operating systems. One of the main reasons for having a standardized programming language like Fortran is to ensure that a program will be portable from one computer to another. As long as a computer can understand Fortran, it can run any program written in Fortran. Program portability has always been important, but it is all the more vital now that the Internet makes it possible for a program to be distributed with great ease to large numbers of different computers.
Fortran, then, is a standard programming language to allow general-purpose computers to solve specialized problems. The newcomer to Fortran may be surprised to learn that it is supposed to be an easy language, intended to be used not by experts in computer science but by those whose primary specialism is in some other discipline that might be a branch of engineering, physics, chemistry, mathematics, or the environmental and human sciences. For this reason—as well as for reasons of program portability—Fortran does not involve the programmer in the “low-level” technical details of exactly how a particular computer system is designed and how it stores and manipulates information internally. This philosophy is an aspect of what is sometimes called “data abstraction”—allowing the programmer to think of data in terms of idealized concepts (such as that of a real number) while not having to think about how that data is represented physically within a particular computer’s hardware. If Fortran is not easy to learn, it is because, in response to demand by its users, it has become a sophisticated and powerful modern programming language and is very extensive. However, most programming tasks can be achieved with recourse to only a fraction of Fortran’s features, and most program-writers do not attempt to memorize the whole language. The skill lies not in knowing all of Fortran, but in knowing what it can express and being able to refer to what you want when you want it.
A program written in Fortran will be translated (or “compiled”, to use the technical term) into the appropriate low-level machine code by the computer system itself. This leaves the programmer free to concentrate on solving the real problem in hand, whether it be in aeronautics, economic forecasting or whatever. Programmers can cultivate their expertise in their specialist subjects and their involvement with computer science need not extend much beyond the strictly defined boundaries of the programming language. The purpose of a programming language, like that of any language, is that the user should not have to cross its boundaries. The compilation of a program from Fortran into machine code is carried out by a part of the system known as a “compiler”. A compiler is itself a piece of software, i.e. a computer program, but one that would not normally be written in Fortran. For all its merits, Fortran is not usually the ideal language for writing system software or compilers. Other languages, such as C, come into their own for such purposes. Compiling a program is not the same as running it: compilation puts instructions into a form understandable to the computer, but only when the compiled program is “run” will the instructions be carried out.
Portability between computers has already been mentioned above, but portability in time is just as important as portability in space. There is now an enormous investment in existing programs written by the scientific community in the past using earlier versions of Fortran. To preserve that investment, Fortran 90 was designed for backwards-compatibility with the previous standard version, FORTRAN 77; and apart from the deletion of a few of the obsolete features Fortran 95 is compatible with Fortran 90. Consequently, Fortran 95 includes a number of archaic features that would not have been included in the language if it were being designed from scratch without the burden of history. The archaic features should be avoided by a newcomer; they are no longer functionally necessary, and in this book they are covered only for the sake of completeness.
What we mean by a “program” generally takes the form of a length of text divided into lines. There might be a couple of dozen lines in a very simple program, or a few hundred in a medium-sized one, or many tens of thousands of lines in sophisticated programs written and maintained by teams of programmers.
Any program will have a starting point and an end point, and instructions between the start and the end will be “executed” in turn when the program runs. Many of the instructions will manipulate data in some way—multiplying numbers together, for example, or concatenating strings of text. However, some instructions can permit a program to branch out into alternative paths, and so in practice most programs are not executed in a fixed linear sequence of instructions. There can be branches, loops, conditionally executed blocks of instructions, and other structures, and a program may have several different possible points of termination.

1.3
Program structure

In general a Fortran program consists of a number of separate “program units”, namely
  • A “main program” When the program as a whole is run, it always starts at the first statement of the main program.
  • An arbitrary number of other program units These can be called into play when needed by special statements within the main program or within other program units.
A short program could consist only of a main program, but in practice there are usually several program units. Apart from the main program, the other program units fall into two classes:
  • “external subprograms”, or “external procedures” (introduced in Chapter 7).
  • “modules” (dealt with in Chapter 10) that can, for example, be used to contain data that will be made available to different subprograms.
There is a third class of program unit, now outmoded and unnecessary, known as BLOCK DATA. Throughout the chapters of this book, small type like this is used when referring to old Fortran features whose use is no longer recommended.
The difference between a module and an external subprogram is that a module contains information made permanently accessible to the program unit that invokes it: invocation is by a once-and-for-all declaration at the start of that program unit. A subprogram, on the other hand, is invoked dynamically (and perhaps repetitively) by action statements during program execution.
Subprograms can be invoked from the main program or from other subprograms. There is even the possibility of “recursive” subprograms that invoke themselves. Subprograms fall into two classes, namely “subroutines” and “functions”, differing according to exactly how they are invoked. Normally subroutines are used to switch the flow of execution of the program, while functions are used for repeated calculations.
It should be noted that the terms “procedure” and “subprogram” are synonyms in the Fortran context. They are used interchangeably in this book.
There is another kind of subprogram not so far mentioned, namely a “module subprogram”. A module subprogram is a procedure encapsulated within a module. Module subprograms, like external subprograms, are classified as either subroutines or functions, but putting a group of them together in a module can be a useful way of setting out a group of subprograms that have a special relationship with one another and need access to a shared pool of data. Technically, module subprograms are not classified as program units in their own right: it is the module that contains them that is classed as a “program unit”.

1.4
Intrinsic procedures

“Intrinsic procedures” are a kind of toolkit provided for the programmer in Fortran. They are a generalpurpose set of subprograms that can be invoked from any Fortran program. For example, to calculate the square root of a number a program only needs to use the expression SQRT(…), inserting the number in question between the brackets. SQRT is a Fortran “keyword” that simply causes the square root to be calculated. Procedures like SQRT are just there, waiting in the background in case they are needed. They are an intrinsic part of the Fortran language. There are well over 100 intrinsic procedures in Fortran 95, and almost all of them (including SQRT) take the form of a “function”, like a mathematical function, and are called “intrinsic functions”. A function is a procedure that can be referred to simply by mentioning its name, even in the middle of a complicated expression; for example SQRT(21–SQRT(25)) is an acceptable Fortran expression that calls the SQRT function twice and is equal to 4.0.
Fortran’s intrinsic procedures include several standard mathematical functions such as the common trigonometrical and hyperbolic functions. They also include some very useful procedures for frequently needed numeric manipulations, such as finding the absolute magnitude of a number or picking out the largest from a given set of numbers. There are other intrinsic procedures that operate not on numbers but on strings of characters. Yet others are useful for checking up on the characteristics of the computer being used: for example the intrinsic inquiry function PRECISION will tell your program what is the limit of your computer’s decimal precision. Inquiry functions like this would be unnecessary if all computers were identical, but are very valuable for writing programs to be portable between computers with different characteristics.
There are still other intrinsic procedures that are used for handling vectors and matrices, and for manipulating binary “bits”, and for tasks such as the generating of random numbers. They are introduced gradually through this book, but are all listed in Appendix F.

1.5
Statements

A Fortran program or subprogram consists of an ordered sequence of “statements”, and the statements can carry out different sorts of tasks. Some statements (“assignment” statements) give a value to a variable quantity: for example

n = 256

is an assignment statement that sets a variable called n equal (for the time being!) to 256. Assignment statements always have an = sign in them. Other kinds of statement are indicated by special keywords, for example a statement that writes out data will begin with the keyword WRITE. Fortran statement types are listed in Appendix E; it is a long list, and rather bewildering at first sight, but in practice most programs, like most people’s conversation, is built up predominantly out of statements of relatively few kinds. Statements can be divided into two important classes: “declarative” statements and “executable” statements. The declarative statements can be imagined to take effect once and for all when the program is compiled: they establish what kinds of data are to be manipulated by the program (or subprogram), allow memory space to be allotted to data, and may set up certain relationships between items of data.
The executable statements take full effect only when the program is run: they are action statements that cause items of data to be altered or moved in some way, or that make decisions. Executable statements are sometimes called “procedural” statements and, when the program runs, the computer proceeds through them in turn.
Within a program or subprogram, the declarative statements must be grouped together at the beginning and they are followed by the executable statements in sequence. The difference between a declarative statement and an executable statement is quite fundamental. It is a little like the difference between an adjective and a verb in English. Consider the following program:

Cinderella is beautiful

Sisters are ugly, and coarse, and there are two of them

The Prince is rich

Dress Cinderella

Nullify the Sisters

Take Cinderella to the dance

Marry the Prince

The first three of these statements do not actually do anything but they set the scene. They could have been stated in any order. They are declarative. The last four statements are executable, in the sense that changes are made to some of the entities referred to in the first three statements. For the executable statements, the order is important.
All Fortran program units have the same basic structure as the Cinderella example above. However, one should be aware that there are different styles and fashions in programming and some programmers prefer to emphasize declarative aspects of programming while others prefer the procedural approach. Fortran is historically a procedural language and earlier versions of it were much more limited in their repertoire of declarative statements than Fortran 95 is.
There are several kinds of declarative statement in ...

Table of contents