315 53 2MB
English Pages 835 Year 1992
Fortran 90 Handbook Complete ANSI / ISO Reference Jeanne C. Adams Walter S. Brainerd Jeanne T. Martin Brian T. Smith Jerrold L. Wagener Intertext Publications McGraw-Hill Book Company
Library of Congress Catalog Card Number 91-77211
Copyright © 1992 by Jeanne C. Adams, Walter S. Brainerd, Jeanne T. Martin, Brian T. Smith, and Jerrold L. Wagener. All rights reserved. Printed in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this book may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system without the prior written permission of the authors and the publisher.
10 9 8 7 6 5 4 3 2 1
ISBN 0-07-000406-4 Intertext Publications/Multiscience Press, Inc. One Lincoln Plaza New York, NY 10023 McGraw-Hill Book Company 1221 Avenue of the Americas New York, NY 10020
Composition by UNICOMP
Preface The Fortran 90 Handbook is a definitive and comprehensive guide to Fortran 90 and its use. Fortran 90, the latest standard version of Fortran, has many excellent new features that will assist the programmer in writing efficient, portable, and maintainable programs. The Fortran 90 Handbook is an informal description of Fortran 90, developed to provide not only a readable explanation of features, but also some rationale for the inclusion of features and their use. In addition, “models” give the reader better insight as to why things are done as they are in the language. This handbook is intended for anyone who wants a comprehensive survey of Fortran 90, including those familiar with programming language concepts but unfamiliar with Fortran. Experienced Fortran 77 programmers will be able to use this volume to assimilate quickly those features in Fortran 90 that are not in Fortran 77 (Fortran 90 is a superset of Fortran 77). Chapter 0 provides a brief overview of several of the most important features that are new in Fortran 90. Chapters 1–14 correspond to Sections 1–14 in the standard. (The standard is the complete official description of the language, but it is written in a legally airtight, formal style without tutorial material and can be difficult to understand in places.) The handbook and the standard can be examined in parallel for insights into the Fortran language. This makes it feasible to use this handbook to “decipher” the standard, and this is an ideal use of this book. Although the handbook is written for use in conjunction with the standard, it is also designed as a practical stand-alone description of Fortran 90. In the interest of readability, a few of the more obscure aspects of the standard may
iii
not be treated rigorously; any such cases should not impact the usefulness of this handbook in describing Fortran 90. On the other hand, in places where the standard is not completely clear, a reasonable interpretation is often given, together with ways to implement and program that will avoid potential problems due to misinterpretation of the standard. Of course, if information is being sought to understand a fine point of compiler implementation, settle a bet, resolve a court case, or determine the answer to a Fortran trivia question, the standard itself should be considered the final authority. The syntactic features of the language are described completely in the appendices, and these can serve as continual concise references for Fortran 90.
Other Sources of Information Other parts of the book can be used to help find information.
• •
• • •
Each of the intrinsic functions is described in detail in Appendix A, although a general discussion of the intrinsic functions is included in Chapter 13. The complete syntax of Fortran 90 may be found in Appendix B. The syntax rules are numbered exactly as they are in the Fortran standard. There is a cross reference that lists, for each nonterminal syntactic term, the number of the rule in which it is defined, and all rules in which it is referenced. Appendix C contains a listing of the obsolescent features. The index is unusually comprehensive. There is an index of examples, giving the location of program examples that illustrate the use of many Fortran 90 features.
For an informal and tutorial approach to learning Fortran 90, the book, Programmers Guide to Fortran 90, Second Edition, by Brainerd, Goldberg, and Adams (Unicomp, Albuquerque, NM, 1993) is more appropriate.
Style of the Programming Examples In order to illustrate many features of the language and as many uses of these features as possible, no single particular style has been used when writing the examples. In many cases, the style illustrated is not necessarily one that the authors recommend.
iv
FORTRAN User’s Guide
Acknowledgments Material in the appendices of this book was developed by the ANSI committee X3J3 and the ISO committee SC22/WG5 for inclusion in the Fortran 90 standard ISO/IEC 1539 : 1991. This material is reprinted with the permission of the International Standards Organization. Comments provided by Charles Goldberg have increased the accuracy and readability of this book enormously.
Copyright Fortran 90 Handbook is reproduced herein with the permission of McGrawHill, Inc., Copyright 1992, by Walter S. Brainerd, Jeanne C. Adams, Jeanne T. Martin, Brian T. Smith, and Jerrold L. Wagener. All rights reserved.
Printed Copies Printed copies of this book may be obtained by ordering from Unicomp, Inc. 1874 San Bernardino Ave NE Albuquerque, NM 87122 USA +1-505-275-0800 +1-505-856-1501 (fax) Visit the Fortran market: http://www.fortran.com/fortran The home page includes how to order this book in hard copy.
Jeanne C. Adams Walter S. Brainerd, [email protected] Jeanne. T. Martin Brian T. Smith Jerrold L. Wagener January 1992
v
vi
FORTRAN User’s Guide
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iii
Sneak Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why a New Standard? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Not Use Another Language? . . . . . . . . . . . . . . . . . . . Development of Fortran 90 . . . . . . . . . . . . . . . . . . . . . . . . . Fortran 77 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . Extensibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intrinsic and Standard Modules . . . . . . . . . . . . . . . . . . . . . The Fortran 90 Language Standard . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13 15 17 18 19 20 20 21 24
2. Fortran Concepts and Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
2.1 2.2 2.3 2.4 2.5 2.6 2.7
Scope and Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ordering Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27 35 39 43 46 52 57
vii Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener
2.8 Example Fortran 90 Program . . . . . . . . . . . . . . . . . . . . . . . 2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
59 59
3. Language Elements and Source Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
3.1 3.2 3.3 3.4 3.5 3.6 3.7
The Processor Character Set . . . . . . . . . . . . . . . . . . . . . . . . Lexical Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Source Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Rules for Fixed/Free Source Form . . . . . . . . . . . . . . . . . . . The INCLUDE Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Low-Level Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66 69 73 82 83 84 84
4. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
4.1 4.2 4.3 4.4 4.5 4.6 4.7
Building the Data Environment for a Problem Solution . What Is Meant by “Type” in Fortran? . . . . . . . . . . . . . . . . Intrinsic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Structure Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Array Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
91 96 99 110 121 124 127
5. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
131
5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11
viii
Type Declaration Statements. . . . . . . . . . . . . . . . . . . . . . . . Implicit Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Array Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Value Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object Accessibility and Use . . . . . . . . . . . . . . . . . . . . . . . . Procedure Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Automatic Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . NAMELIST Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Storage Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fortran 90 Handbook
Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener
134 142 144 152 155 161 171 174 175 176 186
6. Using Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1 6.2 6.3 6.4 6.5 6.6
197
Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Structure Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointers and Allocatable Arrays. . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
199 201 203 205 215 222
7. Expressions and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . .
227
7.1 7.2 7.3 7.4 7.5 7.6
Introduction to Fortran 90 Expressions . . . . . . . . . . . . . . . Formation of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . Interpretation of Expressions . . . . . . . . . . . . . . . . . . . . . . . Evaluation of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
228 234 276 284 290 303
8. Controlling Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
309
8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8
The Execution Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . Blocks and Executable Constructs . . . . . . . . . . . . . . . . . . . IF Construct and IF Statement . . . . . . . . . . . . . . . . . . . . . . The CASE Construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The DO Construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Branching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Obsolescent Control Statements . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
310 310 312 316 321 333 337 341
9. Input and Output Processing . . . . . . . . . . . . . . . . . . . . . . . . . . .
345
9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8
Records, Files, Access Methods, and Units . . . . . . . . . . . . Data Transfer Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . Execution Model for Data Transfer Statements . . . . . . . . Error and Other Conditions in Input/Output Statements The OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inquiring about Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File Positioning Statements . . . . . . . . . . . . . . . . . . . . . . . . .
Table of Contents
346 359 389 392 395 405 408 417
ix Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener
9.9 Restrictions: I/O Specifiers, List Items, and Statements . 9.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
422 423
10. Input and Output Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
427
10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 10.10 10.11 10.12
Explicit Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Format Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Character String Edit Descriptor Form . . . . . . . . . . . . . . . Formatted Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . File Positioning by Format Control . . . . . . . . . . . . . . . . . . Numeric Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Logical Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Character Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Control Edit Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . List-Directed Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . Namelist Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
429 432 435 436 441 442 455 456 457 465 471 480
11. Program Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
483
11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Internal Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Host Association. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . External Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Block Data Program Units . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
483 485 489 491 496 498 515 516
12. Using Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
521
12.1 12.2 12.3 12.4 12.5 12.6 12.7
x
Procedure Terms and Concepts . . . . . . . . . . . . . . . . . . . . . Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Procedure-Related Statements . . . . . . . . . . . . . . . . . . . . . . Argument Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Procedure Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fortran 90 Handbook
Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener
522 530 535 543 548 573 587
13. Intrinsic Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8 13.9 13.10
593
Intrinsic Procedure Terms and Concepts . . . . . . . . . . . . . . Representation Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inquiry and Numeric Manipulation Functions . . . . . . . . Transfer and Conversion Functions . . . . . . . . . . . . . . . . . . Computation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intrinsic Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Alphabetical List of All Intrinsic Procedures . . . . . . . . . . Specific Names for Generic Intrinsic Procedures . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
594 596 598 601 602 604 606 606 612 615
14. Scope, Association, and Definition . . . . . . . . . . . . . . . . . . . . . .
617
14.1 14.2 14.3 14.4
The Use of Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Definition Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
619 620 629 637
A. Intrinsic Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
645
B. Fortran 90 Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
727
B.1 The Form of the Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . B.2 Syntax Rules and Constraints . . . . . . . . . . . . . . . . . . . . . . . B.3 Cross References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
727 731 758
C. Decremental Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
777
C.1 Deleted Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C.2 Obsolescent Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
777 777
Index of Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
781
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
789
Table of Contents
xi Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener
Source Form Control Structures Numeric Processing
Array Processing
Pointers
Data Structures
Fortran 77
User-Defined Types and User-Defined Operations Procedures Modules Input/Output Features Obsolescent Features
0
Fortran 90 Handbook
Sneak Preview
0
This is Chapter 0. Fortran programmers, particularly old-timers, are accustomed to starting at 1. Prior to Fortran 77, a DO loop had to be executed at least once, and array subscripts started with one. Even though these restrictions were eliminated in Fortran 77, arrays had to have at least one element. In Fortran 90, the programmer can create strings of length zero and arrays of size zero. So this Chapter 0 will help Fortran 90 programmers get accustomed to other possibilities for the number 0. Seriously, though, the main reason for starting with Chapter 0 is that the remaining chapters of this book correspond with the fourteen chapters of the Fortran standard and are numbered 1–14 as they are in the standard. Chapter 0 provides the opportunity for a brief introduction to some of the exciting new features of Fortran 90. The pie chart on the opposing page illustrates how Fortran 90 is made up of Fortran 77 plus several new features. The relative sizes of the slices are determined from the detailed syntax rules in Appendix B—each pie slice is roughly proportional to the number of syntax rules describing that part of Fortran 90. Thus the pie gives one measure of the relative complexity of the different parts of Fortran 90. It only indicates structural (syntactic) complexity, however, and should not be taken as an indication of conceptual (semantic) complexity; structural and conceptual complexity may or may not be related. It also should not be taken as an indication of implementation effort (which also may or may not be related). In fact, the cost pattern of implementation may be somewhat machine–architecture dependent or dependent upon the particular design strategy. Although this measure is crude, it shows clearly that the majority of statements in Fortran 90 are already familiar to Fortran 77 programmers.
1
0 Despite these caveats, the structure of a language is an important part of learning it and using it; therefore, the pie chart provides useful information about Fortran 90. The main purpose of this sneak preview is to provide a brief introduction to each new feature slice of the Fortran pie. Just a glance at the names of the new features should be enough to convince anyone that they will become very important to Fortran programmers. Fortran always has been considered the premier language in scientific and engineering fields requiring numeric computations. The new features of Fortran 90 continue to enhance Fortran for these applications and also to extend the language in significant ways to other areas now very important in scientific and engineering programming. This chapter is a sneak preview of some of these features, illustrating briefly why it will be important to master and use these facilities.
Fortran 77 One of the most important features of Fortran 90 is that it contains all of the features of Fortran 77. There are four relatively obscure things that are processor dependent in Fortran 77, but completely specified in Fortran 90; these are described in Section 1.5. If a program uses one of these features and it was done differently on a particular implementation than the way chosen for Fortran 90, this program could behave differently under Fortran 90. Otherwise, all standard-conforming Fortran 77 programs should run using a Fortran 90 compiler and produce equivalent results.
Source Form and Names In Fortran 90 there is a new source form for which particular positions have no special meaning, names may have up to 31 characters and use the underscore character, blanks have significance in some circumstances, a semicolon may be used to separate multiple statements on one line, and comments may occur on any line following an exclamation (!). The old source form is still available and most of these new features are also available when using the old source form. SWAP_INTEGERS is a simple example of a subroutine written using the new source form.
2
Fortran 90 Handbook
0 SUBROUTINE SWAP_INTEGERS (ARG_A, ARG_B) INTEGER, INTENT (INOUT) :: ARG_A, ARG_B INTEGER :: TEMP ! New form of declaration TEMP = ARG_A; ARG_A = ARG_B; ARG_B = TEMP END
If the above code were written so that each line began in position 7 or beyond, it would also be acceptable as old source form.
Control Structures Control structures have not been neglected; Fortran now has a complete suite of modern control structures. A CASE construct has been added. The DO construct has been improved significantly and now may utilize the CYCLE and EXIT statements. In addition, the DO construct can have a WHILE control clause, an iterative control clause, or no control clause. The DO, IF, and CASE constructs may have construct names to help identify the constructs, which is especially useful when constructs are nested. The following example illustrates a CASE construct and a DO construct that contains an IF construct and an EXIT statement. SEARCH_LOOP: DO I = 1, TABLE_SIZE IF (ITEM == TABLE (I)) THEN LOCATION = I EXIT SEARCH_LOOP END IF END DO SEARCH_LOOP SELECT CASE (COLOR (LOCATION)) CASE ("RED") STOP CASE ("YELLOW") PRINT *, "Look out!" CALL CAUTION CASE ("GREEN") CALL GO END SELECT
Numeric Processing One of the most difficult aspects of porting Fortran programs is the specification of numeric precision. Fortran 90 contains new features that allow the programmer to specify precision in a more portable manner and to inquire
3
0 about properties of the precision used by a processor. It is possible to declare that real variables R1 and R2 have at least 10 decimal digits and a range extending to at least 10 30 by using the declaration REAL (SELECTED_REAL_KIND (10, 30)) :: R1, R2
The values of R1 and R2 may be represented using single precision on some machines and double precision on others. The actual precision and range of any real variables can be determined using intrinsic functions provided for this purpose. Other intrinsic functions allow the programmer to manipulate the components of a real value in a portable manner. For example, the intrinsic function SPACING can be used to determine the convergence of an iterative process. CONVERGED = ( ABS (X0 - X) < 2 * SPACING (X) )
It is also possible to indicate a minimum required range of an integer value in a declaration, as illustrated by the following example. INTEGER (SELECTED_INT_KIND (5)) :: I1, I2
In this case, the Fortran system must select an integer representation (if one is available) that allows the integer variables I1 and I2 to have all integer values between – 10 5 and 10 5 ; if the programmer limits values assigned to I1 and I2 to this range, portability is guaranteed.
Array Processing Many Fortran programs process arrays of data. These programs usually are full of DO loops that process array elements one at a time. In fact, the more natural way to think of the process is that it performs some operation on the whole array. Allowing the programmer to manipulate arrays of data in this manner is perhaps the single most important enhancement in Fortran 90. This reflects not only the benefit of expressing array computations in a more natural manner, but also the development of computers having array processing hardware to achieve high processing speeds. In Fortran 90 it is possible to treat a whole array as a single object. For example, suppose A, B, and C are 10 × 10 arrays of real values. For each element of B, the statement A = 2 * B + C
4
Fortran 90 Handbook
0 doubles its value, adds it to the corresponding element of C, and places the result in the corresponding element of A. Parts of an array may be referenced. For example, PRINT *, A (3, :), B (:, 1:99:2)
prints the third row of the array A and the odd-numbered columns of B. A section of an array may be given another name with the use of a pointer, but that is another story. There is a rich set of new intrinsic functions to process arrays. Users may define array-valued functions, and arrays may be allocated dynamically. This last feature alone will be a tremendous aid to programmers who have had to jump through hoops and often use nonstandard (and nonportable) features in an attempt to manage storage allocation. One use of dynamic allocation is illustrated by a simple example in which an array’s size is determined as the program is executing. REAL, ALLOCATABLE :: A (:,:) . . . READ *, N ALLOCATE (A (N,N))
There are many other new features designed to assist in array processing, such as the WHERE construct and the use of arrays with pointers.
Pointers The pointer features of Fortran 90 permit data to be accessed and processed dynamically. REAL, POINTER :: A (:,:) . . . READ *, N ALLOCATE (A (N,N))
Note that, except for the replacement of the keyword “ALLOCATABLE” with the keyword “POINTER”, this example is identical to the previous one in the section on arrays. Everything that can be done with allocatable arrays can also be done with pointers, but allocatable arrays can be used in simple situations where pointer concepts are not required. Any object may have the pointer attribute; it is not limited to arrays.
5
0 In addition, the effect of assignment can be achieved without the movement of data; and dynamic structures, such as linked lists and trees, can be created and processed. In most cases, a pointer may be thought of as an alias to some data object. For example, a pointer may “point to” or “alias” a row of an array, a simple variable, a component of a structure, or an entire data structure. REAL, TARGET :: A (100,100) REAL, POINTER :: ITH_ROW (:), CORNERS (:,:), INNER (:,:) . . . ITH_ROW => A (I, :) CORNERS => A (1:100:99, 1:100:99) INNER => A (2:99, 2:99)
In Fortran 90, pointers may point only to objects having the target attribute. This is to allow all optimization techniques in those cases that do not involve pointers.
Data Structures In the past, scientific and engineering programs typically involved large amounts of computation; if there were a large amount of data, it usually was organized in very simple ways. However, contemporary applications often process large and complex data structures, both numeric and nonnumeric. Fortran 90 provides the programmer with better tools to deal with such data by including data structures in the language. Unlike an array, the components of a Fortran 90 data structure do not have to be of the same data type. Data structures are introduced into a program with a type definition, such as the following: TYPE EMPLOYEE ! An employee’s name may have up CHARACTER (LEN = 20) ! A social security number (SSN) INTEGER (SELECTED_INT_KIND (9)) ! SALARY may be up to $1M and is REAL (SELECTED_REAL_KIND (8, 6)) END TYPE EMPLOYEE
to 20 characters. :: NAME has nine digits. :: SSN kept to the penny. :: SALARY
Variables declared to be type EMPLOYEE have three components, NAME, SSN, and SALARY, each of a different data type. In the following example LARRY, MOE, and CURLY are structures of type EMPLOYEE.
6
Fortran 90 Handbook
0 TYPE (EMPLOYEE) :: LARRY, MOE, CURLY
An entire structure can be referenced by using its name, such as MOE. Individual components can be manipulated as follows: MOE % SSN = 123456789
User-Defined Types and Operators Programmers may extend the Fortran 90 built-in facilities in two ways. New data types may be built from the intrinsic types, and the programmer may extend the built-in operators, such as + and //, to other data types. In addition, new operators may be defined for any data types. These facilities allow the programmer to define abstract data types and facilitate the utilization of the object-oriented programming paradigm in Fortran. For example, it is possible to define a new type called MATRIX and extend the operator ∗ to mean matrix multiplication between two variables declared to be type MATRIX. TYPE (MATRIX) :: M1, M2, M3 . . . M3 = M1 * M2
For this example, it is assumed that the type MATRIX has been defined as the type EMPLOYEE was defined in the example in "Data Structures" in this chapter. The form of each defined type must be a structure; in this case, it could be a structure with one component—a two-dimensional array of reals, for example, or it could be some sort of linked structure representing a sparse matrix. The operation (∗) representing matrix multiplication is defined by a function with an operator interface.
Procedures There are several new features in Fortran 90 that facilitate the use of procedures. Functions can extend existing operators and define new ones. Subroutines are used to redefine assignment for user-defined types, if desired. Procedure arguments may be made optional and keywords may be used when calling procedures, allowing them to be listed in any order. Default values may be specified for missing optional arguments. SUBROUTINE CONCERT (LOCATION, TIME, BAND, BACKUP) INTEGER, OPTIONAL :: LOCATION, TIME, BAND, BACKUP
7
0 . . .
With this declaration any of the following could be used to call the subroutine: CALL CONCERT (1, 2, 3, 4) CALL CONCERT (1, BACKUP=4) CALL CONCERT (TIME=2, LOCATION=1)
A procedure interface block is used to describe the characteristics of an external procedure and its arguments, give a procedure a generic name, define a new operator or extend an old one, or define a new form of assignment. Procedure interface blocks are necessary in some cases to allow the correct procedure call to be generated; their use also will permit the compiler to check that procedure calls are correct, particularly to check that argument types match. This provides the capability to guarantee the integrity of a procedure call and to guard against errors. The programmer may define generic procedures in Fortran 90. Here are the subprograms and the interface blocks that create a generic function CUBE_ROOT that will find the cube root of either a real or double precision value. INTERFACE
CUBE_ROOT
FUNCTION S_CUBE_ROOT(X) REAL :: S_CUBE_ROOT REAL, INTENT(IN) :: X END FUNCTION S_CUBE_ROOT FUNCTION D_CUBE_ROOT(X) DOUBLE PRECISION, INTENT(IN) :: X DOUBLE PRECISION :: D_CUBE_ROOT END FUNCTION D_CUBE_ROOT END INTERFACE
FUNCTION S_CUBE_ROOT(X) REAL, INTENT(IN) :: X REAL :: S_CUBE_ROOT S_CUBE_ROOT = ... END FUNCTION
8
Fortran 90 Handbook
S_CUBE_ROOT
0 FUNCTION D_CUBE_ROOT(X) DOUBLE PRECISION, INTENT(IN) :: X DOUBLE PRECISION :: D_CUBE_ROOT D_CUBE_ROOT = ... END FUNCTION
D_CUBE_ROOT
Fortran 90 also has recursion. RECURSIVE SUBROUTINE QUICK_SORT (NUMBERS, START, END) . . . NEW_START = . . . NEW_END = . . . IF (START