Fortran 90 Handbook, complete ANSI-ISO reference 0070004064, 9780070004061

Contains a complete description of the standard programming language FORTRAN 90, covering all of its features in a forma

315 53 2MB

English Pages 835 Year 1992

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Preface......Page 3
Style of the Programming Examples......Page 4
Printed Copies......Page 5
Table of Contents......Page 7
Sneak Preview......Page 13
Source Form and Names......Page 14
Numeric Processing......Page 15
Array Processing......Page 16
Pointers......Page 17
Data Structures......Page 18
Procedures......Page 19
Modules......Page 21
Input/ Output Features......Page 22
Language Architecture......Page 23
1.1 History......Page 25
1.2 Why a New Standard?......Page 27
1.3 Why Not Use Another Language?......Page 29
1.4 Development of Fortran 90......Page 30
1.5 Fortran 77 Compatibility......Page 31
1.7 Intrinsic and Standard Modules......Page 32
1.8 The Fortran 90 Language Standard......Page 33
1.9 References......Page 36
2.1 Scope and Association......Page 39
2.2 Program Organization......Page 47
2.3 Data Environment......Page 51
2.4 Program Execution......Page 55
2.5 Terms......Page 58
2.6 Summary of Forms......Page 64
2.7 Ordering Requirements......Page 69
2.9 Summary......Page 71
Language Elements......Page 77
3.1 The Processor Character Set......Page 78
3.2 Lexical Tokens......Page 81
3.3 Source Form......Page 85
3.4 Rules for Fixed/ Free Source Form......Page 94
3.5 The INCLUDE Line......Page 95
3.7 Summary......Page 96
Data Types......Page 101
4.1 Building the Data Environment for a Problem Solution......Page 103
4.2 What Is Meant by “Type” in Fortran?......Page 108
4.3 Intrinsic Data Types......Page 111
4.4 Derived Types......Page 122
4.5 Structure Constructors......Page 133
4.6 Array Constructors......Page 136
4.7 Summary......Page 139
Declarations......Page 143
5.1 Type Declaration Statements......Page 146
5.2 Implicit Typing......Page 154
5.3 Array Properties......Page 156
5.4 Pointer Properties......Page 164
5.5 Value Attributes......Page 167
5.6 Object Accessibility and Use......Page 173
5.7 Procedure Properties......Page 183
5.8 Automatic Data Objects......Page 186
5.9 NAMELIST Statement......Page 187
5.10 Storage Association......Page 188
5.11 Summary......Page 198
Using Data......Page 209
6.1 Constants and Variables......Page 211
6.2 Substrings......Page 213
6.3 Structure Components......Page 215
6.4 Arrays......Page 217
6.5 Pointers and Allocatable Arrays......Page 227
6.6 Summary......Page 234
Expressions and Assignment......Page 239
7.1 Introduction to Fortran 90 Expressions......Page 240
7.2 Formation of Expressions......Page 246
7.3 Interpretation of Expressions......Page 288
7.4 Evaluation of Expressions......Page 296
7.5 Assignment......Page 302
7.6 Summary......Page 315
Controlling Execution......Page 321
8.2 Blocks and Executable Constructs......Page 322
8.3 IF Construct and IF Statement......Page 324
8.4 The CASE Construct......Page 328
8.5 The DO Construct......Page 333
8.6 Branching......Page 345
8.7 Obsolescent Control Statements......Page 349
8.8 Summary......Page 353
Input and Output Processing......Page 357
9.1 Records, Files, Access Methods, and Units......Page 358
9.2 Data Transfer Statements......Page 371
9.3 Execution Model for Data Transfer Statements......Page 401
9.4 Error and Other Conditions in Input/ Output Statements......Page 404
9.5 The OPEN Statement......Page 407
9.6 The CLOSE Statement......Page 417
9.7 Inquiring about Files......Page 420
9.8 File Positioning Statements......Page 429
9.9 Restrictions: I/ O Specifiers, List Items, and Statements......Page 434
9.10 Summary......Page 435
Input and Output Editing......Page 439
10.1 Explicit Formatting......Page 441
10.2 Format Specifications......Page 444
10.3 Character String Edit Descriptor Form......Page 447
10.4 Formatted Data Transfer......Page 448
10.5 File Positioning by Format Control......Page 453
10.6 Numeric Editing......Page 454
10.7 Logical Editing......Page 467
10.8 Character Editing......Page 468
10.9 Control Edit Descriptors......Page 469
10.10 List- Directed Formatting......Page 477
10.11 Namelist Formatting......Page 483
10.12 Summary......Page 492
11.1 Overview......Page 495
11.2 Main Program......Page 497
11.3 Internal Procedures......Page 501
11.4 Host Association......Page 503
11.5 External Subprograms......Page 508
11.6 Modules......Page 510
11.7 Block Data Program Units......Page 527
11.8 Summary......Page 528
Using Procedures......Page 533
12.1 Procedure Terms and Concepts......Page 534
12.2 Subroutines......Page 542
12.3 Functions......Page 547
12.4 Procedure- Related Statements......Page 555
12.5 Argument Association......Page 560
12.6 Procedure Interfaces......Page 585
12.7 Summary......Page 599
Intrinsic Procedures......Page 605
13.1 Intrinsic Procedure Terms and Concepts......Page 606
13.2 Representation Models......Page 608
13.3 Inquiry and Numeric Manipulation Functions......Page 610
13.4 Transfer and Conversion Functions......Page 613
13.5 Computation Functions......Page 614
13.6 Array Functions......Page 616
13.8 Alphabetical List of All Intrinsic Procedures......Page 618
13.9 Specific Names for Generic Intrinsic Procedures......Page 624
13.10 Summary......Page 627
Scope, Association, and Definition......Page 629
14.1 The Use of Names......Page 631
14.2 Scope......Page 632
14.3 Association......Page 641
14.4 Definition Status......Page 649
Intrinsic Procedures......Page 657
A. 2 ACHAR (I)......Page 658
A. 5 ADJUSTR (STRING)......Page 659
A. 7 AINT (A, KIND)......Page 660
A. 8 ALL (MASK, DIM)......Page 661
A. 9 ALLOCATED (ARRAY)......Page 662
A. 11 ANY (MASK, DIM)......Page 663
A. 12 ASIN (X)......Page 664
A. 13 ASSOCIATED (POINTER, TARGET)......Page 665
A. 14 ATAN (X)......Page 666
A. 16 BIT_ SIZE (I)......Page 667
A. 17 BTEST (I, POS)......Page 668
A. 19 CHAR (I, KIND)......Page 669
A. 20 CMPLX (X, Y, KIND)......Page 670
A. 23 COSH (X)......Page 671
A. 24 COUNT (MASK, DIM)......Page 672
A. 25 CSHIFT (ARRAY, SHIFT, DIM)......Page 673
A. 26 DATE_ AND_ TIME (DATE, TIME, ZONE, VALUES)......Page 674
A. 27 DBLE (A)......Page 676
A. 29 DIM (X, Y)......Page 677
A. 30 DOT_ PRODUCT (VECTOR_ A, VECTOR_ B)......Page 678
A. 32 EOSHIFT (ARRAY, SHIFT, BOUNDARY, DIM)......Page 679
A. 33 EPSILON (X)......Page 681
A. 35 EXPONENT (X)......Page 682
A. 38 HUGE (X)......Page 683
A. 40 IAND (I, J)......Page 684
A. 41 IBCLR (I, POS)......Page 685
A. 43 IBSET (I, POS)......Page 686
A. 45 IEOR (I, J)......Page 687
A. 46 INDEX (STRING, SUBSTRING, BACK)......Page 688
A. 47 INT (A, KIND)......Page 689
A. 48 IOR (I, J)......Page 690
A. 50 ISHFTC (I, SHIFT, SIZE)......Page 691
A. 52 LBOUND (ARRAY, DIM)......Page 692
A. 55 LGE (STRING_ A, STRING_ B)......Page 694
A. 56 LGT (STRING_ A, STRING_ B)......Page 695
A. 58 LLT (STRING_ A, STRING_ B)......Page 696
A. 60 LOG10 (X)......Page 697
A. 62 MATMUL (MATRIX_ A, MATRIX_ B)......Page 698
A. 64 MAXEXPONENT (X)......Page 700
A. 65 MAXLOC (ARRAY, MASK)......Page 701
A. 66 MAXVAL (ARRAY, DIM, MASK)......Page 702
A. 67 MERGE (TSOURCE, FSOURCE, MASK)......Page 703
A. 68 MIN (A1, A2, A3, ...)......Page 704
A. 70 MINLOC (ARRAY, MASK)......Page 705
A. 71 MINVAL (ARRAY, DIM, MASK)......Page 706
A. 73 MODULO (A, P)......Page 708
A. 74 MVBITS (FROM, FROMPOS, LEN, TO, TOPOS)......Page 709
A. 75 NEAREST (X, S)......Page 710
A. 77 NOT (I)......Page 711
A. 78 PACK (ARRAY, MASK, VECTOR)......Page 712
A. 80 PRESENT (A)......Page 713
A. 81 PRODUCT (ARRAY, DIM, MASK)......Page 714
A. 84 RANDOM_ SEED (SIZE, PUT, GET)......Page 716
A. 85 RANGE (X)......Page 717
A. 86 REAL (A, KIND)......Page 718
A. 88 RESHAPE (SOURCE, SHAPE, PAD, ORDER)......Page 719
A. 89 RRSPACING (X)......Page 720
A. 91 SCAN (STRING, SET, BACK)......Page 721
A. 92 SELECTED_ INT_ KIND (R)......Page 722
A. 93 SELECTED_ REAL_ KIND (P, R)......Page 723
A. 95 SHAPE (SOURCE)......Page 724
A. 98 SINH (X)......Page 725
A. 100 SPACING (X)......Page 726
A. 101 SPREAD (SOURCE, DIM, NCOPIES)......Page 727
A. 103 SUM (ARRAY, DIM, MASK)......Page 728
A. 104 SYSTEM_ CLOCK (COUNT, COUNT_ RATE, COUNT_ MAX)......Page 730
A. 107 TINY (X)......Page 731
A. 108 TRANSFER (SOURCE, MOLD, SIZE)......Page 732
A. 109 TRANSPOSE (MATRIX)......Page 733
A. 111 UBOUND (ARRAY, DIM)......Page 734
A. 112 UNPACK (VECTOR, MASK, FIELD)......Page 735
A. 113 VERIFY (STRING, SET, BACK)......Page 736
B. 1 The Form of the Syntax......Page 739
B. 2 Syntax Rules and Constraints......Page 743
B. 3 Cross References......Page 770
C. 2 Obsolescent Features......Page 789
Index of Examples......Page 793
Index......Page 801
Recommend Papers

Fortran 90 Handbook, complete ANSI-ISO reference
 0070004064, 9780070004061

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

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