# Metcalf Modern Fortran Explained 7th

Post on 26-Oct-2014

363 views

Embed Size (px)

TRANSCRIPT

NUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION Series Editors A. M. STUART E. SULI

NUMERICAL MATHEMATICS AND SCIENTIFIC COMPUTATION Books in the series

Monographs marked with an asterisk ( ) appeared in the series Monographs in Numerical Analysis which is continued by the current series. For a full list of titles please visit http://www.oup.co.uk/academic/science/maths/series/nmsc

J. H. Wilkinson: The algebraic eigenvalue problem I. Du, A. Erisman, and J. Reid: Direct methods for sparse matrices M. J. Baines: Moving nite elements J. D. Pryce: Numerical solution of Sturm-Liouville problems

C. Schwab: p- and hp- nite element methods: theory and applications in solid and uid mechanics J. W. Jerome: Modelling and computation for applications in mathematics, science, and engineering A. Quarteroni and A. Valli: Domain decomposition methods for partial dierential equations G. Em Karniadakis and S. J. Sherwin: Spectral/hp element methods for CFD I. Babuka and T. Strouboulis: The nite element method and its reliability s B. Mohammadi and O. Pironneau: Applied shape optimization for uids S. Succi: The lattice Boltzmann equation: for uid dynamics and beyond P. Monk: Finite element methods for Maxwells equations A. Bellen and M. Zennaro: Numerical methods for delay dierential equations J. Modersitzki: Numerical methods for image registration M. Feistauer, J. Felcman, and I. Strakraba: Mathematical and computational methods for s compressible ow W. Gautschi: Orthogonal polynomials: computation and approximation M. K. Ng: Iterative methods for Toeplitz systems M. Metcalf, J. Reid, and M. Cohen: Fortran 95/2003 explained G. Em Karniadakis and S. Sherwin: Spectral/hp element methods for computational uid dynamics, second edition D. A. Bini, G. Latouche, and B. Meini: Numerical methods for structured Markov chains H. Elman, D. Silvester, and A. Wathen: Finite elements and fast iterative solvers: with applications in incompressible uid dynamics M. Chu and G. Golub: Inverse eigenvalue problems: theory, algorithms, and applications J.-F. Gerbeau, C. Le Bris, and T. Leli`vre: Mathematical methods for the magnetohydrodynamics of e liquid metals G. Allaire and A. Craig: Numerical analysis and optimization: an introduction to mathematical modelling and numerical simulation K. Urban: Wavelet methods for elliptic partial dierential equations B. Mohammadi and O. Pironneau: Applied shape optimization for uids, second edition K. Bhmer: Numerical methods for nonlinear elliptic dierential equations: a synopsis o M. Metcalf, J. Reid, and M. Cohen: Modern Fortran Explained

Modern Fortran ExplainedMichael MetcalfFormerly of CERN, Geneva, Switzerland

John ReidJKR Associates, Oxfordshire

and Malcolm CohenThe Numerical Algorithms Group, Oxfordshire

1

Great Clarendon Street, Oxford ox2 6dp Oxford University Press is a department of the University of Oxford. It furthers the Universitys objective of excellence in research, scholarship, and education by publishing worldwide in Oxford New York Auckland Cape Town Dar es Salaam Hong Kong Karachi Kuala Lumpur Madrid Melbourne Mexico City Nairobi New Delhi Shanghai Taipei Toronto With oces in Argentina Austria Brazil Chile Czech Republic France Greece Guatemala Hungary Italy Japan Poland Portugal Singapore South Korea Switzerland Thailand Turkey Ukraine Vietnam Oxford is a registered trade mark of Oxford University Press in the UK and in certain other countries Published in the United States by Oxford University Press Inc., New York c Michael Metcalf, John Reid, and Malcolm Cohen 2011 The moral rights of the authors have been asserted Database right Oxford University Press (maker) First edition published 1987 as Fortran 8x Explained Second edition published 1989 Third edition published 1990 as Fortran 90 Explained Fourth edition published 1996 as Fortran 90/95 Explained Fifth edition published 1999 Sixth edition published 2004 as Fortran 95/2003 Explained This edition published 2011 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, without the prior permission in writing of Oxford University Press, or as expressly permitted by law, or under terms agreed with the appropriate reprographics rights organization. Enquiries concerning reproduction outside the scope of the above should be sent to the Rights Department, Oxford University Press, at the address above You must not circulate this book in any other binding or cover and you must impose the same condition on any acquirer British Library Cataloguing in Publication Data Data available Library of Congress Cataloging in Publication Data Library of Congress Control Number: 2010941705 Printed in Great Britain on acid-free paper by CPI Antony Rowe, Chippenham, Wiltshire ISBN 9780199601417 (Hbk) 9780199601424 (Pbk) 1 3 5 7 9 10 8 6 4 2

3

PrefaceFortran remains one of the principal languages used in the elds of scientic, numerical, and engineering programming, and a series of revisions to the standard dening successive versions of the language has progressively enhanced its power and kept it competitive with several generations of rivals. Beginning in 1978, the technical committee responsible for the development of Fortran standards, X3J3 (now PL22.3 but still informally called J3), laboured to produce a new, muchneeded modern version of the language, Fortran 90. Its purpose was to promote portability, reliability, maintainability, and efcient execution. . . on a variety of computing systems. That standard was published in 1991, and work began in 1993 on a minor revision, known as Fortran 95. Subsequently, and with the same purpose, a further major upgrade to the language was prepared by J3 and the international committee, WG5. This revision, which included object-oriented programming features, is now known as Fortran 2003. This has now been followed by a further revision, Fortran 2008, and, once again, it seems appropriate to prepare a denitive informal description of the language that it denes. This continues the series of editions of this book the two editions of Fortran 8x Explained that described the two drafts of the standard (1987 and 1989), Fortran 90 Explained that described the Fortran 90 standard (1990), two editions of Fortran 90/95 Explained that included Fortran 95 too (1996 and 1999) and Fortran 95/2003 (2004), with its added chapters on Fortran 2003. In that nal endeavour, a third co-author was welcomed. In this book, an initial chapter sets out the background to the work on new standards, and the nine following chapters describe Fortran 95 (less its obsolescent features and the redundant Fortran 77 features whose use we deprecate) in a manner suitable both for grasping the implications of its features, and for writing programs. We include the allocatable array extensions that were originally published as an ISO Technical Report and are now part of Fortran 2003, since they have been implemented in Fortran 95 compilers for many years. Some knowledge of programming concepts is assumed. In order to reduce the number of forward references and also to enable, as quickly as possible, useful programs to be written based on material already absorbed, the order of presentation does not always follow that of the standard. In particular, we have chosen to defer to appendices the description of features that are ofcially labelled as redundant (some of which were deleted from the Fortran 95 standard) and other features whose use we deprecate. They may be encountered in old programs, but are not needed in new ones. Chapter 11 describes another part of Fortran 2003 that was originally dened by an ISO Technical Report. This is followed, in Chapters 12 to 17, by descriptions of the other features

vi

Preface

dened by the Fortran 2003 standard. Chapter 18 describes a part of Fortran 2008 that was originally dened by an ISO Technical Report and two further chapters describe the other new features of Fortran 2008. The structure of the book thus allows the reader to distinguish clearly between Fortran 95 (plus allocatable array extensions), Fortran 2003, and the new Fortran 2008 features. Note that, apart from a small number of deletions, each of the languages Fortran 77, Fortran 90, Fortran 95, Fortran 2003, and Fortran 2008 is a subset of its successor. In order to make the book a complete reference work, it concludes with seven appendices. They contain, successively, a list of the intrinsic procedures, a description of various features whose use we deprecate and do not describe in the body of the book, a description of obsolescent and deleted features, advice on avoiding compilation cascades, an extended example illustrating the use of object orientation, a glossary of Fortran terms, and solutions to most of the exercises. It is our hope that this book, by providing complete descriptions of Fortran 95, Fortran 2003 and Fortran 2008, will continue the helpful role that earlier editions played for the corresponding versions of the standard, and that it will serve as a long-term reference work for the modern Fortran programming language. Malcolm Cohen wishes to thank the Numerical Algorithms Group (NAG) for its encouragement during the writing of this book.

Conventions used in this book Fortran displayed text is set in typewriter font:integer :: i, j

and a line consisting of a colon indicates omitted lines:subroutine sort : end subroutine sort

Informal BNF terms are in italics: if (scalar-logical-expr) action-stmt Square brackets in italics indicate optional items: end if [name] and an ellipsis represents an arbitrary number of repeated items: [ case selector [name]] block] ... The italic letter b signies a blank character. Corrections to any signicant errors detected in this book will be made available in the les edits.ps and edits.pdf at ftp://ftp.numerical.rl.ac.uk/pub/MRandC.

This page intentionally left blank

Contents1 Whence Fortran? 1.1 Introduction . . . . . . . . . . . . . 1.2 Fortrans early history . . . . . . . . 1.3 The drive for the Fortran 90 standard 1.4 Language evolution . . . . . . . . . 1.5 Fortran 95 . . . . . . . . . . . . . . 1.6 Extensions to Fortran 95 . . . . . . 1.7 Fortran 2003 . . . . . . . . . . . . . 1.8 Fortran 2008 . . . . . . . . . . . . . 1.9 Conformance . . . . . . . . . . . . Language elements 2.1 Introduction . . . . . . . . . . . . 2.2 Fortran character set . . . . . . . 2.3 Tokens . . . . . . . . . . . . . . . 2.4 Source form . . . . . . . . . . . . 2.5 Concept of type . . . . . . . . . . 2.6 Literal constants of intrinsic type . 2.6.1 Integer literal constants . . 2.6.2 Real literal constants . . . 2.6.3 Complex literal constants . 2.6.4 Character literal constants 2.6.5 Logical literal constants . 2.7 Names . . . . . . . . . . . . . . . 2.8 Scalar variables of intrinsic type . 2.9 Derived data types . . . . . . . . 2.10 Arrays of intrinsic type . . . . . . 2.11 Character substrings . . . . . . . . 2.12 Objects and subobjects . . . . . . 2.13 Pointers . . . . . . . . . . . . . . 2.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 3 4 4 5 6 7 7 9 9 9 10 11 13 14 14 15 17 17 19 20 20 21 23 26 27 27 29

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

3

Expressions and assignments 33 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

x Contents

3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 4

Scalar numeric expressions . . . . . . . . . . . . . Dened and undened variables . . . . . . . . . . Scalar numeric assignment . . . . . . . . . . . . . Scalar relational operators . . . . . . . . . . . . . Scalar logical expressions and assignments . . . . Scalar character expressions and assignments . . . Structure constructors and scalar dened operators Scalar dened assignments . . . . . . . . . . . . . Array expressions . . . . . . . . . . . . . . . . . . Array assignment . . . . . . . . . . . . . . . . . . Pointers in expressions and assignments . . . . . . The nullify statement . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

. . . . . . . . . . . . .

34 37 38 38 39 41 42 45 46 48 48 51 51 55 55 55 57 59 62 63 67 67 68 69 69 70 73 73 75 76 76 77 77 78 80 80 82 83 85 85 88 89 90

Control constructs 4.1 Introduction . . . . . . . . . . 4.2 The if construct and statement 4.3 The case construct . . . . . . . 4.4 The do construct . . . . . . . 4.5 The go to statement . . . . . . 4.6 Summary . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

5

Program units and procedures 5.1 Introduction . . . . . . . . . . . . . . . . . 5.2 Main program . . . . . . . . . . . . . . . . 5.3 The stop statement . . . . . . . . . . . . . 5.4 External subprograms . . . . . . . . . . . . 5.5 Modules . . . . . . . . . . . . . . . . . . . 5.6 Internal subprograms . . . . . . . . . . . . 5.7 Arguments of procedures . . . . . . . . . ....