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 iii 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 iv FORTRAN User’s Guide 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. v 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 McGraw- Hill, 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, walt@fortran.com Jeanne. T. Martin Brian T. Smith Jerrold L. Wagener January 1992 vi FORTRAN User’s Guide vii Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Sneak Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1 History. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 Why a New Standard?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.3 Why Not Use Another Language? . . . . . . . . . . . . . . . . . . . 17 1.4 Development of Fortran 90 . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.5 Fortran 77 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6 Extensibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.7 Intrinsic and Standard Modules . . . . . . . . . . . . . . . . . . . . . 20 1.8 The Fortran 90 Language Standard . . . . . . . . . . . . . . . . . . 21 1.9 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2. Fortran Concepts and Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.1 Scope and Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2 Program Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.3 Data Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.4 Program Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.5 Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.6 Summary of Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.7 Ordering Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 viii Fortran 90 Handbook Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener 2.8 Example Fortran 90 Program . . . . . . . . . . . . . . . . . . . . . . . 59 2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3. Language Elements and Source Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.1 The Processor Character Set . . . . . . . . . . . . . . . . . . . . . . . . 66 3.2 Lexical Tokens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 3.3 Source Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.4 Rules for Fixed/Free Source Form . . . . . . . . . . . . . . . . . . . 82 3.5 The INCLUDE Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 3.6 Low-Level Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4.1 Building the Data Environment for a Problem Solution . 91 4.2 What Is Meant by “Type” in Fortran? . . . . . . . . . . . . . . . . 96 4.3 Intrinsic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 4.4 Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 4.5 Structure Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 4.6 Array Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 5.1 Type Declaration Statements. . . . . . . . . . . . . . . . . . . . . . . . 134 5.2 Implicit Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 5.3 Array Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.4 Pointer Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 5.5 Value Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 5.6 Object Accessibility and Use. . . . . . . . . . . . . . . . . . . . . . . . 161 5.7 Procedure Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 5.8 Automatic Data Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 5.9 NAMELIST Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 5.10 Storage Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 5.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Table of Contents ix Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener 6. Using Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 6.1 Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 6.2 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 6.3 Structure Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 6.4 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 6.5 Pointers and Allocatable Arrays. . . . . . . . . . . . . . . . . . . . . 215 6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 7. Expressions and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 7.1 Introduction to Fortran 90 Expressions . . . . . . . . . . . . . . . 228 7.2 Formation of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 234 7.3 Interpretation of Expressions . . . . . . . . . . . . . . . . . . . . . . . 276 7.4 Evaluation of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 284 7.5 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 8. Controlling Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 8.1 The Execution Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 8.2 Blocks and Executable Constructs . . . . . . . . . . . . . . . . . . . 310 8.3 IF Construct and IF Statement . . . . . . . . . . . . . . . . . . . . . . 312 8.4 The CASE Construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 8.5 The DO Construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 8.6 Branching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 8.7 Obsolescent Control Statements. . . . . . . . . . . . . . . . . . . . . 337 8.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 9. Input and Output Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 9.1 Records, Files, Access Methods, and Units . . . . . . . . . . . . 346 9.2 Data Transfer Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . 359 9.3 Execution Model for Data Transfer Statements . . . . . . . . 389 9.4 Error and Other Conditions in Input/Output Statements 392 9.5 The OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 9.6 The CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 9.7 Inquiring about Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 9.8 File Positioning Statements. . . . . . . . . . . . . . . . . . . . . . . . . 417 x Fortran 90 Handbook Copyright © 1992 J. Adams, W. Brainerd, J. Martin, B. Smith, and J. Wagener 9.9 Restrictions: I/O Specifiers, List Items, and Statements . 422 9.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 10. Input and Output Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 10.1 Explicit Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 10.2 Format Specifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 10.3 Character String Edit Descriptor Form . . . . . . . . . . . . . . . 435 10.4 Formatted Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 10.5 File Positioning by Format Control . . . . . . . . . . . . . . . . . . 441 10.6 Numeric Editing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 10.7 Logical Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 10.8 Character Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 10.9 Control Edit Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 10.10 List-Directed Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 10.11 Namelist Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 10.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 11. Program Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 11.2 Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 11.3 Internal Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 11.4 Host Association. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 11.5 External Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 11.6 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498 11.7 Block Data Program Units. . . . . . . . . . . . . . . . . . . . . . . . . . 515 11.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516 12. Using Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 12.1 Procedure Terms and Concepts . . . . . . . . . . . . . . . . . . . . . 522 12.2 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 12.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 12.4 Procedure-Related Statements . . . . . . . . . . . . . . . . . . . . . . 543 12.5 Argument Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 12.6 Procedure Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 12.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 [...]... in Fortran, the Fortran standards committee decided to include the entire previous standard (Fortran 77) in Fortran 90 Even though the standard describes a category called “deleted” features, there aren’t any; as mentioned earlier, no Fortran 77 feature has been removed Fortran 90 restricts the behavior of some features that are processor dependent in Fortran 77 Therefore, a standard-conforming Fortran. .. processor-dependent features may conform to the Fortran 90 standard and yet behave differently than with some Fortran 77 systems In the following situations, the Fortran 90 interpretation may be different from that of Fortran 77 1 Fortran 90 has more intrinsic functions than does Fortran 77 and has a few intrinsic subroutines Therefore, a standard-conforming Fortran 77 program may have a different interpretation... controversial when Fortran 77 was introduced, it proved to be controversial later—so much so that no Fortran 77 features have been removed in Fortran 90 As soon as the technical development of Fortran 77 was completed, ANSI X3J3 and International Standards Organization (ISO) WG5 turned their attention to the next revision, which is now called Fortran 90 and is the subject of this book The work on Fortran 90 began... 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... 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. .. changes 18 Fortran 90 Handbook Copyright © 1992 J Adams, W Brainerd, J Martin, B Smith, and J Wagener 1 made as a result of these comments The technical work was finished in 1 990, and the language became known as Fortran 90 It took until 1991 for it to become an official international standard (ISO/IEC 1539 : 1991) and it took until 1992 to become a U S national standard (ANSI X3.198-1992) 1.5 Fortran. .. 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... use Fortran to solve their problems However, it is possible for a Fortran programmer to learn the new features of Fortran 90 gradually, picking features to master when the effort is justified by the improved problem-solving tools that are made available For these reasons, Fortran may well be the programming language of choice for scientists and engineers for many years 1.4 Development of Fortran 90 During... precise syntax allowed, refer to Appendix B, which contains all of the syntax rules of the Fortran 90 standard 1.8 The Fortran 90 Language Standard The Fortran 90 standard (ISO/IEC 1539 : 1991) describes the syntax and semantics of a programming language However, the standard addresses certain aspects of the Fortran processing system, but does not address others When specifications are not covered by... conforming 1.8.2 Processor Conformance In the Fortran 90 standard, the term “processor” means the combination of a Fortran compiler and the computing system that executes the code A processor conforms to the standard if it processes any standard-conforming 22 Fortran 90 Handbook Copyright © 1992 J Adams, W Brainerd, J Martin, B Smith, and J Wagener 1 program, provided the Fortran program is not too large or . 10020 Composition by UNICOMP iii 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 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. 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