28
Fortran 90 Tutorial Michael Metcalf CN Division, CERN, CH 1211, Geneva 23, Switzerland 1 Language Elements 1 2 Expressions and Assignments 6 3 Control Statements 8 4 Program Units and Procedures 9 5 Array handling 12 6 Pointers 16 7 Specification Statements 20 8 Intrinsic Procedures 22 9 Input/Output 23 Index 25 Full details of all the items in this tutorial can be found in Fortran 90/95 Explained, by M. Metcalf and J. Reid, (Oxford, 1996), the book upon which it has been based. Fortran 90 contains the whole of FORTRAN 77—only the new features are described in this tutorial. The tutorial is also available on WWW using the URL . The author wishes to thank Michel Goossens (CERN/CN) for his helpful and skilful as- sistance in preparing this tutorial. Version of October 1995

f90 tutorial

Embed Size (px)

DESCRIPTION

Tutorial Fortran 90

Citation preview

  • Fortran 90 Tutorial

    Michael Metcalf

    CN Division, CERN, CH 1211, Geneva 23, Switzerland

    1 Language Elements 12 Expressions and Assignments 63 Control Statements 84 Program Units and Procedures 95 Array handling 126 Pointers 167 Specification Statements 208 Intrinsic Procedures 229 Input/Output 23

    Index 25

    Full details of all the items in this tutorial can be found in Fortran 90/95 Explained, byM. Metcalf and J. Reid, (Oxford, 1996), the book upon which it has been based.Fortran 90 contains the whole of FORTRAN 77only the new features are described inthis tutorial.

    The tutorial is also available on WWW using the URL !"#%$'&

    .

    The author wishes to thank Michel Goossens (CERN/CN) for his helpful and skilful as-sistance in preparing this tutorial.

    Version of October 1995

  • Fortran 90 Tutorial 1

    1. Language ElementsThe basic components of the Fortran language are its character set. The members are: the letters and (which are equivalent outside a character context); the numerals ; the underscore and the special characters

    !

    #"

    %$

    &(' ) * + , - .

    /10%2

    From these components, we build the tokens that have a syntactic meaning to the compiler. There are six classes of token:

    Label: 3546Constant: 3546 /789:; "=Keyword: ??@A5B%C?D

    Operator: $$

    Name: EF" %G 00%HJI%KML" (can have up to 31 characters, including a ).Separator: NOPQRSP T - +U)

    From the tokens, we can build statements. These can be coded using the new free source form which does not require positioningin a rigid column structure, as follows:

    V%WMXA%B>Y5@5X

    EJKZL=["=["[5K

    )

    EJKZMLa=M

    $

    %K

    E3

    )

    EJKZL=

    $

    5Kj3

    E3

    )

    0>=K

    `

    k

    *

    E54

    )

    EgKZL>=M

    $

    %K]3

    E54

    )

    0>=K

    `

    &_BJ`

    LaEbLaEcT[">KL=K

    `

    DX5lmV5WXA%B>YF@%X

    EJKZML>=["[%K

    Note the trailing comments and the trailing continuation mark. There may be 39 continuation lines, and 132 characters per line.Blanks are significant. Where a token or character constant is split across two lines:

    EJK%ZKM"%n

    *

    *

    FdS0

    !

    G

    05Zo

    ">=

    *

    *

    EJKZLS=

    !

    a leading * on the continued line is also required.Automatic conversion of source form for existing programs can be carried out by A@%XpD%qB (CERN Program Library Q904). Itsoptions are:

    significant blank handling; indentation;

    A@%XB>YgX%WD

    replaced by DX%lTlS@ ; name added to subprogram DX%l statement; and

    YJXBDr5D%q

    4 etc. syntax converted.

    The source code of the A@%XpD%qB program can be obtained by anonymous ftp to sFZ [[ Z [ I (130.246.8.23). The direc-tory is tI %u $ q and the file name is [" G 0%ZK n .

  • Fortran 90 Tutorial 2

    Fortran has five intrinsic data types. For each there is a corresponding form of literal constant. For the three numeric intrinsictypes they are:YJXBDr%D%q

    Examples are:3

    64

    :9:

    35

    for the default kind; but we may also define, for instance for a desired range of to , a named constant, say K2" oK0aE :YgXBDr%D5q

    f%qM

    u

    DBD5q

    K2"

    oK0aE

    D?DA%BD%l

    YJXB

    YJX%l

    7

    that allows us to define constants of the form3546

    7

    5K2"

    oK0aE

    35%K2"

    oK0E

    Here, K2" oK0aE is the kind type parameter; it can also be a default integer literal constant, like3546

    7

    4

    but use of an explicit literal constant would be non-portable.The YJX%l function supplies the value of a kind type parameter:

    YJX%l

    3M

    YJX%l

    3F%K2"

    oK0aEM

    and the qXr5D function supplies the actual decimal range (so the user must make the actual mapping to bytes):qMXr%D

    =

    D?DA5BD%l

    qMD?

    YJX%l

    for at least 9 decimal digits of precision and a range of to , allowing3

    :

    "=

    Also, we have the intrinsic functions

    YJX%l

    3

    :

    "=

    f5qDAaY

    Y5@%X

    3

    :

    "=>

    qMXr%D

    =

    The numeric types are based on model numbers with associated inquiry functions (whose values are independent of the valuesof their arguments). These functions are important for writing portable numerical software.lYFraYJB

    Number of significant digitsDf

    YJ?@%X

    Almost negligible compared to one (real)War%D

    Largest numberu

    Df@5XDXB

    Maximum model exponent (real)u

    YgXDf@5XDXB

    Minimum model exponent (real)f%qMDAaY

    YF@%X

    Decimal precision (real, complex)q5lY

    Base of the modelqXr%D

    Decimal exponent rangeB>YgXe

    Smallest postive number (real)

  • Fortran 90 Tutorial 3

    The forms of literal constants for the two non-numeric data types are:AM%qA%BD%q

    !

    EJKZML>=

    !

    '

  • Fortran 90 Tutorial 4

    Definitions may refer to a previously defined type:BefD

    t"LK

    qD?

    o

    DX%l BefD

    t"L>K

    BefD

    KZL>=

    0

    BefD

    /t"L>Kk\

    [

    DX%l BefD

    KZML>=

    0

    and for a variable of type KZML>= 0 , as inBefD

    KZLS=

    0>UK

    we then have components of type t1"L>K :

    K

    )

    K

    )

    K

    )

    [

    which, in turn, have ultimate components of type ZM0 :

    K

    )

    )

    K

    )

    )

    o K

    )

    )

    05K[

    We note that the ) qualifier was chosen rather than because of ambiguity difficulties.Arrays are considered to be variables in their own right. Given

    qD?

    j3F>

    YJXBDr5D%q

    lY

    u

    DX

    YF@%X

    #

    35\_

    8

    8

    >

    dSt

    (the latter an example of the syntax that allows grouping of attributes to the left of and of variables sharing those attributesto the right), we have two arrays whose elements are in array element order (column major), but not necessarily in contiguousstorage. Elements are, for example,

    j3 j#Ls>

    and are scalars. The subscripts may be any scalar integer expression. Sections are

    j#L

    sS

    &

    Z=K

    `

    Whole arrays and array sections are array-valued objects. Array-valued constants (constructors) are available:

    # 3]U4\U6\

    7

    8

    >

    # #L L

    3]

    h4> >

    # T# 3]U4\U6m>UL

    3 35> >

    # # L

    3]h35> >

    # #

    35L\ L

    3] 3F> >

    making use of the implied-lS@ loop notation familiar from I/O lists. A derived data type may, of course, contain array compo-nents:

    BefD

    KZL

    _G

    0%ZK0

    DX%l BefD

    KZMLt

    0%K

    BefD

    KZLt

    0%K

    lY

    u

    DX

    Y5@%X

    3

    K

    so that

    K\#4>

    &

    E5[

    5Z Q EgKZJI[%KgI>Z0>

    K\#4>

    )

    G

    0%ZK0

    &

    %ZZ5om["Fd5t"10>KT"%n E5[

    5Z

  • Fortran 90 Tutorial 5

    There are some other interesting character extensions. Just as a substring as inA%qMA%BD%q

    ;

    >

    lY

    u

    DX

    Y5@%X

    9

    >

    t1%=0

    t5=0j#s>S#L

    L>

    &

    E#I

    EgKZL>=

    was already possible, so now are the substrings!

    a3546

    789:;\!

    #L

    L>

    o"I

    )

    1FdS0j3

    4>

    Also, zero-length strings are allowed:

    t%=0jQs>>#L

    La3

    &_

    05Z"

    0=K

    `

    EgKZL>=

    Finally, there are some new intrinsic character functions:A%q YJA5q

    n"5Z

    AaYY

    E50%K1

    %lW

    B? %lW

    B%q

    ?DX

    B5qY

    u

    YJX%lD

    E3] EF4\

    CA

    /B%qWD

    qDfDB

    A%X

    n"%Z "0 "%n E505K

    B%qY

    u

    pD%qYJVe

    n"%Zb

    "%n E505K

  • Fortran 90 Tutorial 6

    2. Expressions and AssignmentsThe rules for scalar numeric expresions and assignments, as known from FORTRAN 77, are extended to accommodate thenon-default kinds we encountered in chapter 1. Thus, the mixed-mode numeric expression and assignment rules incorporatedifferent kind type parameters in an expected way:

    Z0

    4

    L>KM0%=0%Z

    Z0

    3

    converts integer to a real value of the same kind as real1; the result is of same kind, and is converted to the kind of real2 forassignment.For scalar relational operations, there is a set of new, alternative operators:

    , ,

    - -

    so we can write expressions such asYJV

    #

    ,

    X%l

    Lm

    sS

    B DX&

    n"5ZiIadS0%ZL[

    G

    %ZL

    0aE

    n

    %=

    &

    n"5Z

    "%=L[

    iG

    %ZL

    0in

    %=

    In the case of scalar characters, two old restrictions are lifted. GivenA%qMA%BD%q

    ;

    hZ0aEQI

    K

    it is now legal to write

    Z0aE#I

    K\#6

    8

    Z0aE#I

    K\3

    6S

    &

    "

    G

    0%Z

    t

    "%2M0

    $

    Z0aE#I

    K\#6

    6S

    Z0aE#I

    K\#6

    4S

    &

    "TaEE5L%=Jada0>KT"%n I

    EJKZLS=

    For an operation between derived-data types, or between a derived type and an intrinsic type, we must define the meaning ofthe operator. (Between intrinsic types, there are intrinsic operations only.) Given

    BefD

    EgKZL>=

    YJXBDr5D%q

    0>=K

    `

    A%qMA%BD%q

    ;

    >

    G

    I0

    DX%l BefD

    EJKZLS=

    A5qA%BD5q

    [

    `

    %Z>3] [

    `

    %Z4 [

    `

    %Z6

    BefD

    EJKZML>=iEgKZ>3] EJKZ4\ EgKZ6

    we can write

    EJKZM6

    EJKZS35aEJKZ4

    &

    dFIjEJK

    $

    0%nL3

    &

    dFIjEJK

    $

    0%nL3

    RG

    0[5K"%Z4

    the two expresions are equivalent only if appropriate parentheses are added as shown. In each case, we have to provide, in amodule, procedures defining the operator and assignment, and make the association by an interface block, also in the module(we shall return to this later).

  • Fortran 90 Tutorial 7

    For the moment, here is an example of an interface for string concatenationYJXBD%qVA5DT@%fD%qMB@Fq

    Q>

    u

    @FlW?DOf%qS@A5D%lWqD

    EJKZLS=["1[%K

    DX%l YJXBD%qVMA%D

    and an example of part of a module containing the definitions of character-to-string and string to character assignment. Thestring concatenation function was shown already in part 1.u

    @JlW?D

    EgKZL>=M%KoJt0

    BefD

    EJKZLS=

    YJXBDMr%D%q

    0=K

    `

    A%qMA%BD%q

    ?DX

    ;

    a

    EgKZL>=M

    $

    %K

    DX5lmBefD

    EJKZL=

    YJXBD%qVMA%DO

    Y5r%X

    u

    DXB

    u

    @FlWM?DOf%qS@A%D%lWqMD

    [%K"aE5aEE5L%=J E5%KM"[EE5L%=J

    DX5lPYgXBD%qVA%D

    YJXBD%qVMA%DT@%fD5qB@Fq

    #>

    u

    @FlWM?DOf%qS@A%D%lWqMD

    EJKZL=["YJX

    WCqS@JWB>YJXD

    [%K"E5aEEFL%=J E [>

    BefD

    S

    YJXBDXB

    YJX

    [

    E

    )

    EgKZL>=M

    $

    %K

    [

    E

    )

    0>=K

    `

    ?DX

    #[>

    DX%l

    WCqa@FWB>YJXD

    [%K"aE5aEE5L%=J

    WCqS@JWB>YJXD

    E5%K"[aEEFL%=J #[ ^E

    BefD

    S

    YJXBDXB

    @FWB

    [

    [

    E

    )

    EJKZLS=

    $

    %KMj3

    E

    )

    0=%K

    `

    DX%l

    WCqS@FWBSYJXD

    E55K"[aEE5L%=g

    V%WXMA%B>Y5@%X

    EJKZLS=["1[%K\E3]^E54>

    DX%l V%WXA5B>Y5@%X

    EgKZL>=M["[%K

    DX%l

    u

    @FlW?D

    EJKZL>=5KoJt0

    Defined operators such as these are required for the expressions that are allowed too in structure constructors (see chapter 1):

    EgKZ>3

    EJKZLS=\#4\ [

    `

    %Z>35[

    `

    %ZM4>

    &

    EJKZgI[%KJI>ZM0O["jEgKZJI[%KM"%Z

    So far we have discussed scalar variables. In the case of arrays, as long as they are of the same shape (conformable), operationsand assignments are extended in an obvious way, on an element-by-element basis. ForqD?

    lY

    u

    DX

    Y5@%X

    35\_4>

    \

    U[

    qD?

    lY

    u

    DX

    Y5@%X

    8

    G

    2

    ?@raY5A%?

    n

    %=\KT"%n E5[

    %Z

    G

    I0

    2

    cG

    3

    &

    2

    `

    "

    0b%ZZ%o

    $$

    L%KL"K05=0%Z

    Df@%XDXB V%qMA%B>Y5@%X

    XD%qD

    B qq

    fAaYJXMr

    fMAaYJXr

    A%?D

    DB

    Df@5XDXB

    Like all FORTRAN 77 functions ( YgX , %C , etc., but not ?DX ), these are array valued for array arguments (i.e. are elemental).

  • Fortran 90 Tutorial 8

    3. Control StatementsThe CASE construct is a replcement for the computed GOTO, but is better structured and does not require the use of statementlabels:

    D?DMA%BTA%

    D

    Iad

    05Z

    & X%W

    u

    CD%q

    "%n KoJt0TLSK0%=0%Z

    A%

    D

    a3

    &

    OG

    I0aE

    0

    "%2

    aE5L5=J

    3

    A%

    D

    #S

    &

    "

    om

    aE5L5=J

    A%

    D

    3

    &

    OG

    I0aE

    "

    G

    0T

    aE5L5=J

    3

    DX%l

    D?DA%B

    Each CASE selector list may contain a list and/or range of integers, character or logical constants, whose values may not overlapwithin or between selectors:

    A%

    D

    3 4

    :

    35

    3

    :

    46S

    A default is available:A%

    DOlDV%W?B

    There is only one evaluation, and only one match.A simplified but sufficient form of the DO construct is illustrated by

    "ISK0%Z

    lS@

    L0%Z

    lS@

    L

    s\^

    &

    "

    omLK0%=05Z>E

    YJV

    A%eMA%?D

    YJV

    DSYJB

    "I>K0%Z

    DX%lTlS@

    L05Z

    DX%lTlS@

    "ISK0%Z

    where we note that loops may be named so that the EXIT and CYCLE statements may specify which loop is meant.Many, but not all, simple loops can be replaced by array expressions and assignments, or by new intrinsic functions. For instance

    K"%K

    lS@

    L

    d _

    K"%K

    K"%K

    jQL>

    DX%lTlS@

    becomes simply

    K"%K

    W

    u\kj d

    \O

  • Fortran 90 Tutorial 9

    4. Program Units and ProceduresIn order to discuss this topic we need some definitions. In logical terms, an executable program consists of one main programand zero or more subprograms (or procedures) - these do something. Subprograms are either functions or subroutines, whichare either external, internal or module subroutines. (External subroutines are what we know from FORTRAN 77.)From an organizational point of view, however, a complete program consists of program units. These are either main programs,external subprograms or modules and can be separately compiled.An internal subprogram is one contained in another (at a maximum of one level of nesting) and provides a replacement for thestatement function:

    WCqS@JWB>YJXD

    "I>K0%Z

    qD?

    Uo

    A@%XBSYJX

    WCqS@JWB>YJXD

    L0%Z

    qD?

    o

    o

    3

    DX%l

    WCqa@FWB>YJXD

    L05Z

    &

    WCqS@FWB>YgXD

    da

    $

    %KM"%Zo

    DX%l

    WCqa@FWB>YJXD

    "I>K05Z

    We say that outer is the host of inner, and that inner obtains access to entities in outer by host association (e.g. to x), whereasy is a local variable to inner. The scope of a named entity is a scoping unit, here outer less inner, and inner.The names of program units and external procedures are global, and the names of implied-DO variables have a scope of thestatement that contains them.Modules are used to package global data (replaces COMMON and BLOCK DATA); type definitions (themselves a scoping unit); subprograms (which among other things replaces the use of ENTRY); interface blocks (another scoping unit, see next article); namelist groups.

    An example of a module containing a type defition, interface block and function subprogram is:

    u

    @FlW?D

    L>K05Z

    G

    %ZL%K

    `

    dS0%KL[

    BefD

    L>KM0%Z

    G

    qD?

    "%20%Z Itt05Z

    DX%l BefD

    LSK0%Z

    G

    YJXBD%qMVA%DT@5fD%qBM@Fq

    u

    @JlW?DOf5qS@A%D%lWqD

    $$

    L>KM0%Z

    G

    E

    DX%l YJXBD5qVA%D

    A@%XBSYJX

    V%WXA%BSY5@%X

    $$

    L>KM0%Z

    G

    E#\

    BefD

    #LSK0%Z

    G

    YJXBDXB

    YgX

    \

    BefD

    #LSK0%Z

    G

    h

    $$

    LSK0%Z

    G

    E

    $$

    L>KM0%Z

    G

    E

    )

    "J20%Z

    )

    "%2M0%Z

    i

    )

    "%205Z

    $$

    L>KM0%Z

    G

    E

    )

    It5t0%Z

    )

    Itt10%Z

    i

    )

    Itt05Z

    DX%l V%WXA5B>Y5@%X

    $$

    L>K05Z

    G

    E

    & V%WXMA%B>Y5@%X

    dS

    $

    5K"%Zo

    DX%l

    u

    @FlWM?D

    L>K0%Z

    G

    5ZL%K

    `

    dM0%KL[

    and the simple statementW

    D

    L>K05Z

    G

    %ZL%K

    `

    dS0%KL5[

    provides use association to all the modules entities. Module subprograms may, in turn, contain internal subprograms.

  • Fortran 90 Tutorial 10

    Arguments

    We may specify the intent of dummy arguments:

    WCqS@JWB>YJXD

    E

    `

    I>nn

    0P/[%Z

    $

    E] [%Z

    $

    EM

    YJXBDr5D%q

    YJXBDXB

    YJX

    1[%Z

    $

    E

    &

    LK

    G

    I0aE

    YJXBDr5D%q

    YJXBDXB

    @FWB

    lY

    u

    DX

    Y5@%X

    [%Z

    $

    E

    [%Z

    $

    E

    &

    "KJtI>K

    G

    I0aE

    Also, INOUT is possible: here the actual argument must be a variable (unlike the default case where it may be a constant).Arguments may be optional:

    WCqS@JWB>YJXD

    dSL["Y5@%X?

    lY

    u

    DX

    Y5@5X

    Itt05Z

    "%20%Z

    allows us to call dSL[" byA%??

    dSL1["/ n

    _Itt0%Z1

    and in daL[" we have someting like:YJV

    f5qD

    DXB

    "%2M0%Z

    B MDX &

    K0aEJKTn"5ZitSZ0aE501[0T"%n [%KJI

    %Z=JIdS0>K

    Arguments may be keyword rather than positional (which come first):A%??

    dSL1[" / ^n

    05HJI

    L5KL0aE

    \

    EJK%ZK

    >

    Optional and keyword arguments are handled by explicit interfaces, that is with internal or module procedures or with interfaceblocks.

    Interface blocks

    Any reference to an internal or module subprogram is through an interface that is explicit (that is, the compiler can see all thedetails). A reference to an external (or dummy) procedure is usually implicit (the compiler assumes the details). However,we can provide an explicit interface in this case too. It is a copy of the header, specifications and DX%l statement of the procedureconcerned, either placed in a module or inserted directly:

    qD?TV%WXMA%B>Y5@%X

    dSLLFdFIad #\

    nJI[>

    &

    Z0%KgI>ZJjE K

    `

    0idSLK05Z

    G

    Q\

    qD?

    YJXBDXB

    QL\

    \

    YJXBD%qMVA%D

    qD?TV%WXA%BSY5@%X

    nJI[j

    qD?

    YJXBDXB

    YJX

    DX5lmV5WXA%B>YF@%X

    nJI1[

    DX%l YJXBD5qVA%D

    qD?

    n

    n

    nJI[]

    &

    L

    G

    "[%KL" "%nTK

    `

    0 IjEF0%ZTnJI[%KML"

    DX%l V%WXA5B>Y5@%X

    daLLFd5Id

    An explicit interface is obligatory for: optional and keyword arguments, f@aYgXBD%q and B%qSr%DB arguments (see later article), af@YJXBD%q function result (later) and new-style array arguments and array functions (later). It allows full checks at compile timebetween actual and dummy arguments.

  • Fortran 90 Tutorial 11

    Overloading and generic interfaces

    Interface blocks provide the mechanism by which we are able to define generic names for specific procedures:YJXBD%qMVA%D

    =MFddS

    &

    =010%ZL[ Fda0

    V%WXA%BSY5@%X

    EJ=MFddSj

    &

    E#t0[L%nL[ JdS0On"%Z

    "%2 t>Z0[LaE5L"

    $

    =JddS\

    DX%l

    DX%l YJXBD5qVA%D

    where a given set of specific names corresponding to a generic name must all be of functions or all of subroutines.We can use existing names, e.g. SIN, and the compiler sorts out the correct association.We have already seen the use of interface blocks for defined operators and assignment (see Part 2).

    Recursion

    Indirect recursion is useful for multi-dimensional integration. To calculate

    G

    "

    Iada0

    LSK0%=Z5K0j no o

    "I

    $

    EM

    we might haveqDAFWq

    YJpDOV5WXA%B>YF@%X

    L>KM0%=Z%KM0j n

    "I

    $

    E

    &hY

    >KM0%=Z%KM0in\

    nZM"Fd

    "I

    $

    E3hKM"

    "I

    $

    E1#4>

    qD?

    L>KM0%=Z%KM0

    YJXBD%qMVA%D

    V%WMXA%B>Y5@5X

    n\

    qD?

    n

    DX5lmV5WXA%B>YF@%X

    n

    DX%l YJXBD5qVA%D

    qD?

    lY

    u

    DX

    YF@%X

    #4>1

    YJXBDXB

    YJX

    "Y5@%X

    LK0%=ZM%K0

    and to integrate f(x, y) over a rectangleV%WXA%BSY5@%X

    no o

    W

    D

    nJI[

    &

    da"

    $

    I

    0 nJI[T["SKLjE nJI[%KL"KM0%=Z%KM0j n

    g

    "I

    $

    E

    DX%l

    Direct recursion is when a procedure calls itself, as inqDAFWq

    YJpDOV5WXA%B>YF@%X

    n[5K"%ZL

    /\

    qD

    WM?B

    Z0E

    YJXBDr5D%q

    Z0aE]

    YJV

    /

    D

    3

    B DX

    Z0E

    3

    D?

    D

    Z0E

    5n[%K"5ZL

    a3

    DX%l YJV

    DX%l

    Here, we note the qD WM?B clause and termination test.

  • Fortran 90 Tutorial 12

    5. Array handlingArray handling is included in Fortran 90 for two main reasons:

    the notational convenience it provides, bringing the code closer to the underlying mathematical form; for the additional optimization opportunities it gives compilers (although there are plenty of opportunities for degrading

    optimization too!).

    At the same time, major extensions of the functionality in this area have been added.We have already met whole arrays in Parts 1 and 2here we develop the theme.

    Zero-sized arrays

    A zero-sized array is handled by Fortran 90 as a legitimate object, without special coding by the programmer. Thus, inlS@

    L

    3]

    #L>

    i

    #L> Tj#L\UL>

    #L

    3

    \

    #L

    3

    \ j#L

    3

    L>

    #L>

    DX%lTlS@

    no special code is required for the final iteration where i = n.We note that a zero-sized array is regarded as being defined; however, an array of shape, say, (0,2) is not conformable with oneof shape (0,3), whereas

    3

    >

    6

    is a valid do nothing statement.

    Assumed-shape arrays

    These are an extension and replacement for assumed-size arrays. Given an actual argument like:qD?

    lY

    u

    DX

    YF@%X

    #

    35\

    4>

    A%??

    E#I

    #>

    the corresponding dummy argument specification defines only the type and rank of the array, not its size. This information hasto be made available by an explicit interface, often using an interface block (see part 4). Thus we write just

    WCqS@FWB>YgXD

    E#I

    $

    >

    qD?

    lY

    u

    DX

    YF@%X

    $

    and this is as if da were dimensioned (11,21). However, we can specify any lower bound and the array maps accordingly. Theshape, not bounds, is passed, where the default lower bound is 1 and the default upper bound is the corresponding extent.

    Automatic arrays

    A partial replacement for the uses to which EQUIVALENCE is put is provided by this facility, useful for local, temporary arrays,as in

    WCqS@JWB>YJXD

    EJ2t Q\

    qD?

    lY

    u

    DX

    YF@%X

    \

    qD?

    lY

    u

    DX

    YF@%X

    YF%D

    #>5

    2"%Z%

    &

    %ZZ%o [%ZM0%K0

    $

    " EJK[F

    2"%Z%

    N

    2"%Z%

    DX%l

    WCqa@FWB>YJXD

    EJ2t

  • Fortran 90 Tutorial 13

    ALLOCATABLE and ALLOCATE

    Fortran 90 provides dynamic allocation of storage; it relies on a heap storage mechanism (and replaces another use of EQUIV-ALENCE). An example, for establishing a work array for a whole program, is

    u

    @FlW?D

    2"%Z%a%ZZ5o

    YJXBDr5D%q

    qD?

    lY

    u

    DX

    YF@%X

    ??@A%B%C?D

    2"%Z%

    DX%l

    u

    @FlWM?D

    f%qS@rFqM

    uidSLKM

    aEEFL%=JadS0K

    EgHZK\#S

    &

    L>KZLjE5L[ nJI[%KL"Y5@%X

    n

    DX5lmf5qS@rFq

    uRKM0aEJK

    WHERE

    Often, we need to mask an assignment. This we can do using the WHERE, either as a statement: D%qD

    #T

    >

    3

    &

    G

    "L

    $O$

    L

    G

    LaE5L"

    o

    (note: test is element-by-element, not on whole array), or as a construct (all arrays of same shape): D%qD

    #T

    >

    3

    N

    DX%l D%qMD

    D%qD

    #T

    >

    3

    D?

    D D%qD

    WSr%D

    #>

    DX%l D%qMD

  • Fortran 90 Tutorial 14

    Array elements

    Simple case: given qMD? lY u DX Y5@5X 35 35> we can reference a single element of as, for instance, j3]h3 . For a derived data type like

    BefD

    KZL

    $

    I

    DX%l BefD

    KZMLt

    0%K

    we can declare an array of that type:

    BefD

    KZL

    )

    $

    I

    is an array of type real, and

    K%Z / 4>

    )

    $

    I #4S

    is an element of it. The basic rule to remember is that an array element always has a subscript or subscripts qualifying at leastthe last name.

    Array subobjects (sections)The general form of subscript for an array section is

    as in

    qMD?

    j35 3F>

    ]#L\h3

    \

    &

    t%ZK "%n "10cZM"%2

    ]3

    d Us>

    &

    t%ZK "%n "10R["

    Iad5

    ]#L\

    &

    2

    `

    "

    0cZM"%2

    ]#L\h3

    6>

    &

    0

    G

    0%ZoRK

    `

    L%Z

    $

    0

    0FdS0SKm"5nRZM"%2

    ]#L\h35

    3

    a3

    &

    Z"%2 L Z0

    G

    0%Z>E50T"%Z

    $

    0%Z

    ]m#m3]

    :

    U6\U4m>h3

    &

    G

    0[%KM"%ZmE#I

    E5[%ZLtSK

    ]3]U4

    33

    4>

    &

    33bLaE

    0%=

    E "%KTZ05n0%Z01[0

    $

    ]

    h3

    :

    &

    Za K2M" EF0[%KL"T

    # 3]U4\h6

    7

    >

    is illegal. Also, a section with a vector subscript must not be supplied as an actual argument to an @FWB or YJX@FWMB dummy argu-ment.

    Arrays of arrays are not allowed:

    K%Z

    )

    $

    I

    &

    L

    0%=

  • Fortran 90 Tutorial 15

    We note that a given value in an array can be referenced both as an element and as a section:

    j3]h3

    &

    E5[

    %Z ZZ0O["Jd5t"0K

    K%Z\o

    G

    I10RLEiKZJI0

    _D

    Jd5t

    0

    YJV

    Xe

    U

    -

    B MDX

    A@FWXB X

    Id

    0%ZT"5nmKZJI10R0

    0FdS0SK>E L %ZZM%o

    u

    p?

    u

    LFd5Iad

    G

    I0TL %ZZ5o

    u

    YJXp?

    uLKSE

    W

    u

    Id "5nm5ZZ%o 0

    0JdS0>K>E

    Array inquiry??@A%BD%l

    ZZ%oT

    "[%KML"PEJKM%KJIjE

    ?%CS@FWMX%l ?

    "520%Z

    $

    LJdS0jE5L"

    "I

    $

    Eb"%n N5ZZ%o

    fD

    `

    t0O"%n %ZZ5o Q"%Z E5[

    5Z

    Y5%D B

    "5K

    Id

    0%Z "%n 0

    0JdS0>K>EbL %ZZ%o

    WCS@FWMX%l W

    tt0%Z

    $

    LJdS0jE5L"

    "I

    $

    Eb"%n N5ZZ%o

    Array construction

    u

    D%qSr5D

    u05Z=0hI

    $

    0%ZOdSaE

    fA

    f

    [F %ZZ%o LSK"T %ZZ%o "%nTZEgKcda

    LFd5Id

    G

    I10RL

  • Fortran 90 Tutorial 16

    6. Pointers

    Basics

    Pointers are variables with the f@aYgXBD%q attribute; they are not a distinct data type (and so no pointer arithmetic is possible):qD?

    f@aYJXBD%q

    _G

    %Z

    They are conceptually a descriptor listing the attributes of the objects (targets) that the pointer may point to, and the address, ifany, of a target. They have no associated storage until it is allocated or otherwise associated (by pointer assignment, see below):

    ??M@A%BD

    G

    %Z

    and they are dereferenced automatically, so no special symbol is required. In

    G

    %Z

    RG

    %Z

    4

    6

    the value of the target of G %Z is used and modified. Pointers cannot be transferred via I/Othe statement%q1YJBD

    \

    G

    %Z

    writes the value of the target of G %Z and not the pointer descriptor itself.A pointer can point to other pointers, and hence to their targets, or to a static object that has the TARGET attribute:

    qD?

    f@aYJXBD%q

    "

    s0[%K

    qD?

    B%qSr%DB

    K%Z=0%K"

    s

    G

    %Z

    -

    "

    s0[%K

    &

    t1"L>K05ZTaEE5L5=JadS0SK

    G

    %Z

    -

    K5Z=0%K"

    s

    but they are strongly typed:YJXBDr%D%q

    f@aYJXBD%q

    L>K

    G

    %Z

    G

    %Z

    -

    LSK

    G

    %Z

    &

    L

    0%=

    RKogt0aE d5IjEJKOdS5K[

    `

    and, similarly, for arrays the ranks as well as the type must agree.A pointer can be a component of a derived data type:

    BefD

    0>KZo

    &

    KoJt0On"%Z E#t5Z>E50idS%KZML

    qD?

    G

    I0

    YJXBDr%D%q

    L

    $

    0

    BefD

    #0SKZo

    f@aYgXBD%q

    0

    K

    &

    1"%K0OZ0[ISZ>E5L"

    DX%l BefD

    0SKZo

    and we can define the beginning of a linked chain of such entries:BefD

    Q0>KZo1

    f@aYgXBD%q

    [

    `

    L

    After suitable allocations and definitions, the first two entries could be addressed as

    [

    `

    L

    )

    G

    I10 [

    `

    L

    )

    0

    K

    )

    G

    I10

    [

    `

    L

    )

    L

    $

    0

    [

    `

    L

    )

    0

    K

    )

    L

    $

    0

    [

    `

    L

    )

    0

    K [

    `

    L

    )

    0

    K

    )

    0

    K

    but we would normally define additional pointers to point at, for instance, the first and current entries in the list.

  • Fortran 90 Tutorial 17

    Association

    A pointers association status is one of undefined (initial state); associated (after allocation or a pointer assignment); disassociated:

    lMD??@A%BD

    /t H

    &

    n"%ZTZ0%KJISZJL>= EJK"%Z5=0

    X5W??>YJVe

    /t H

    &

    n"%Z E50%KKML>=TK"

    !

    I

    !

    Some care has to be taken not to leave a pointer dangling by use of lD??M@A%BD on its target without X%W??>YgVe ing any otherpointer referring to it.The intrinsic function @AaYJBD%l can test the association status of a defined pointer:

    YgV

    @AYJBD%l

    /t"LSK0FZ1

    B DX

    or between a defined pointer and a defined target (which may, itself, be a pointer):YgV

    @AYJBD%l

    /t"LSK0FZ K5Z=0%K

    B DX

    Pointers in expressions and assignments

    For intrinsic types we can sweep pointers over different sets of target data using the same code without any data movement.Given the matrix manipulation o C AR , we can write the following code (although, in this case, the same result could beachieved more simply by other means):

    qD?

    B%qSr5DB

    35 35> [j35\#35>^Z 35>hE

  • Fortran 90 Tutorial 18

    Pointer arguments

    If an actual argument is a pointer then, if the dummy argument is also a pointer, it must have same rank, it receives its association status from the actual argument, it returns its final association status to the actual argument (note: the target may be undefined!), it may not have the INTENT attribute (it would be ambiguous), it requires an interface block.

    If the dummy argument is not a pointer, it becomes associated with the target of the actual argument:qD?

    f@YJXBD%q

    j

    ??@A5BD

    #]

    ;

    \

    ;

    >

    A%??

    E#I

    #>

    WCqS@FWB>YgXD

    E#I

    #[>

    qD?

    [j

    Pointer functions

    Function results may also have the f@aYJXBD%q attribute; this is useful if the result size depends on calculations performed in thefunction, as in

    W

    D

    $

    %K

    `

    $M

    0%Z

    qD?

    3F>

    qD?

    f@YJXBD%q

    o\

    o

    -

    ["FdFt[%K\

    where the module $ %KM ` $ 0%Z containsV%WXA%BSY5@%X

    ["Jd5t[%K

    qD?

    f@YJXBD%q

    ["Jd5t[%K

    qD?

    &

    t>Z"[0

    $

    I>Z0 K"OZ0FdS"

    G

    0

    $

    It

    L[%K0aEknZ"Fd K

    `

    0R5ZZ%o

    YJXBDr5D%q

    & V

    LKZo1

    Z"%2>E1/\

    then

    Z"%2>E

    )

    0

    K

    &

    L

    0%=M

    would be such an object, but with an irregular storage pattern. For this reason they are not allowed. However, we can achievethe same effect by defining a derived data type with a pointer as its sole component:

  • Fortran 90 Tutorial 19

    BefD

    Z"%2

    qD?

    f@YJXBD%q

    Z\

    DX%l BefD

    and then defining arrays of this data type:BefD

    Z"%2

    E1/\ K\/

    where the storage for the rows can be allocated by, for instance,lS@

    L

    3]^

    ??@A5BD

    K #L>

    )

    Z 3

    L>

    &

    "[%K0iZ"52mL "%n

    0>=K

    `

    L

    DX%lTlS@

    The array assignment

    E

    K

    is then equivalent to the pointer assignments

    E#L>

    )

    Z

    -

    K #L>

    )

    Z

    for all components.

    Pointers as dynamic aliases

    Given an arrayqD?

    B5qSr%DB

    K

    1

    0j35\ 35S

    that is frequently referenced with the fixed subscripts

    K

    0] d

    t

    H

    these references may be replaced byqD?

    lY

    u

    DX

    YF@%X

    f@YJXBD%q

    2L

    The subscripts are as those of tar itself. (This replaces yet more of DJWYJp?DXA%D .)The source code of an extended example of the use of pointers to support a data structure can be obtained by anonymous ftp tosFMZ

    [[

    Z

    [

    Ia (130.246.8.23). The directory is

  • Fortran 90 Tutorial 20

    7. Specification StatementsThis part completes what we have learned so far about specification statements.

    Implicit typing

    The implicit typing rules of Fortran 77 still hold. However, it is good practice to explicitly type all variables, and this can beforced by inserting the statementY

    u

    f?>Y5AaYJBTX@5XD

    at the beginning of each prorgam unit.

    PARAMETER attribute

    A named constant can be specified directly by adding the f%q u DBD%q attribute and the constant values to a type statement:qD?

    lY

    u

    DX

    Y5@%X

    Q6>

    f%q

    u

    DBD%q

    nL0

    %$T

    QR

    3

    4

    S

    BefD

    KZL

    DATA statement

    The lB statement can be used also for arrays and variables of derived type. It is also the only way to initialise just parts ofsuch objects, as well as to initialise to binary, octal or hexadecimal values:BefD

    KZL3] K4

    lB

    KS35%KZLmg\ K4

    )

    I

    &

    "

    oT" [

    &

    aEEQIadS0

    $

    0>=K

    `

    A%qA5BD%q

    ?DX

    d

    ?DX

    Q[> [[

    &

    I>KM"FdS%KL[O"

    s0[%K

    qMD?

    D?DA%BD%l

    qD?

    YJXJl

    #4

    f%qDMAaY

    Y5@%X

    >

    &

    t>Z0[LaE5L"

  • Fortran 90 Tutorial 21

    PUBLIC and PRIVATE

    These attributes are used in specifications in modules to limit the scope of entities. The attribute form isqD?

    f%WC?>YFA

    o

    &

    $

    05nI

    K

    YJXBDr%D%q

    f%q1YJpBD

    I

    G

    2

    and the statement form is

    f%WCM?>Y5A

    Uo

    @%fD%qB@Fq

    $$

    f%qYgpBD

    I

    G

    2

    Y5r5X

    u

    DXB

    @%fD%qMB@Fq

    Q>

    The statement form has to be used to limit access to operators, and can also be used to change the overall default:

    f%qYgpBD &

    E505K>E

    $

    0%nI

    KTn"%ZOdS"

    $

    I

    0

    f%WCM?>Y5A

    "

    o%K

    `

    LE

    For a derived data type there are three possibilities: the type and its components are all f5WC?>Y5A , the type is f5WC?>Y5A and itscomponents f%qYJpBD (the type only is visible and one can change its details easily), or all of it is f%qYgpBD (for internal use inthe module only):

    u

    @FlW?D

    dSL0

    f%qYJpBD

    BefD

    f%WC?>Y5A

    U

    LaEgK

    qD?

    Uo

    BefD

    LaEJK1

    f@aYJXBD%q

    0

    K

    DX%l BefD

    LEJK

    BefD

    LaEJK

    KZ00

    DX%l

    u

    @FlWM?D

    dSL10

    USE statement

    To gain access to entities in a module, we use the W D statement. It has options to resolve name clashes if an imported name isthe same as a local one:W

    D

    dSL0

    "[

    LaEgK

    -

    LaEJK

    or to restrict the used entities to a specified set:W

    D

    dSL0

    @%X?e

    LaEJK

    These may be combined:

    W

    D

    dSL0

    @%X?e

    "[

    LaEJK

    -

    LaEJK

  • Fortran 90 Tutorial 22

    8. Intrinsic ProceduresWe have already met most of the new intrinsic functions in previous parts of this series. Here, we deal only with their generalclassification and with those that have so far been omitted.All intrinsic procedures can be referenced using keyword arguments:A%??clMBD

    X%l

    B>Y

    u

    D

    B>Y

    u

    D

    K

    and many have optional arguments. They are grouped into four categories:

    1. elemental work on scalars or arrays, e.g. 5C #> ;2. inquiry independent of value of argument (which maybe undefined), e.g. f%qMDAaY YF@%X #> ;3. transformational array argument with array result of different shape, e.g. qD MfD # ;4. subroutines, e.g. e BD uM A%?@A .

    The procedures not already introduced are:

    Bit inquiry

    C1YJB

    YF%D X

    Iad

    0%Z "%n

    L%K>EbL K

    `

    0idS"

    $

    0

    Bit manipulation

    CMBD

    B C

    L%KTK0aEgKL>=

    YgX%l ?

    "%=L[

    X%l

    Y

    &

    Z0t

    [0aEUt5ZK "%n

    DFWYJp?DXA5D

    Subroutines

    lMBD

    X%l

    B>Y

    u

    D @

    KL

    $

    %K0T

    $

    "%ZOKLJdS0

    u

    p%CYJB

    A

    "tL0aE

    L%K>E

    qMX%lS@

    uM

    X%W

    u

    CMD%q q

    0%KJISZJjEktjE50I

    $

    "%Z

    $

    "Fd Iad

    05Z>E

    qMX%lS@

    uM

    DD%l

    [[0EE K"mE500

    $

    e

    BD

    uM

    A%?@A

    [[0EE K"mEJo>EJK0Jd [

    "[F

  • Fortran 90 Tutorial 23

    9. Input/Output

    Non-advancing input/output

    Normally, records of external, formatted files are positioned at their ends after a read or write operation. This can now be over-ridden with the additional specifiers:%lpXA%D

    ! X@\!

    $

    05nI

    KcLaE

    ! eD

    !

    D@Fq

    0"%Z

    0

    #"tSKL"

    qD5l

    "K05Zi10

    K t>ZLFda0 Iad

    0%Z

    !

    qD%l

    #

    !

    Y

    35>

    !

    t>ZLFdS0Iad

    0%Z

    New edit descriptors

    The first three new edit descriptors are modelled on the Y edit descriptor:C binary,@

    octal, hexadecimal.There are two new descriptors for real numbers:DX

    engineering, multiple-of-three exponent: 4a3 : - 4a3 : D 6 DXM 4>D

    scientific, leading nonzero digit: 4a3 : - 4 3 :5D 4 D 4>and the r edit descriptor is generalized to all intrinsic types ( D V , Y , ? , ).For entities of derived types, the programmer must elaborate a format for the ultimate components:

    BefD

    EJKZML>=

    YJXBDr5D%q

    0>=K

    `

    A%qMA%BD%q

    ?DX

    4> 2M"%Z

    $

    DX%l BefD

    EJKZL>=

    BefD

  • Fortran 90 Tutorial 24

    New specifiers

    On the @%fDX and YJXFW1YqD statements there are new specifiers:

    f@

    YJBSY5@%X

    !

    Y

    ! !/qD>YJX5l ! ! ffDX5l !

    A%B>Y5@5X

    ! qD%l ! ! %qYJBD ! !/qD%l 5qYJBD !

    lD?>Y

    u

    ! f@

    B%qS@%f MD !i! FWS@%BD ! ! X@%XD !

    f%l

    ! eD

    ! ! X@\!

    and on the YJXFWY

  • Indexactual argument, 10, 12, 18aliases, 19ALLOCATE, 13argument, 10array construction, 15array constructors, 4array elements, 14array inquiry, 15array location, 15array manipulation, 15array reduction, 15array reshape, 15array sections, 4array subobjects, 14arrays, 4arrays intrinsic functions, 15arrays of pointers, 18assignment, 6, 13association, 17assumed-shape arrays, 12automatic arrays, 12

    binary, 2, 20bit inquiry, 22bit manipulation, 22blank, 1

    CASE construct, 8CHARACTER, 3, 20comments, 1COMPLEX, 2components, 3constant expressions, 20continuation, 1conversion, 1cursor, 23

    DATA, 20defined operators, 7derived data type, 14, 16, 17, 21DO construct, 8dummy argument, 10, 12, 18

    edit descriptors, 23element, 4, 14elemental, 7elemental operation, 13explicit interface, 10expressions

    initialization of

    , 20specification of

    , 20

    formatted files, 23

    generic interfaces, 11generic names, 11

    heap storage, 13

    hexadecimal, 2, 20

    implicit typing, 20initialization

    of expressions, 20input/output

    new edit descriptors, 23new specifiers, 24non-advancing , 23

    inquiry functions, 2INTEGER, 2INTENT, 1, 7, 9interface, 6interface block, 10, 11, 18intrinsic functions, 7, 15, 17, 20, 22

    keyword, 22kind type parameter, 2

    letters, 1linked chain, 16LOGICAL, 3lower bound, 12, 13

    matrix multiply, 15model numbers, 2modules, 7, 9, 10, 21

    named constant, 2named operators, 6numerals, 1

    octal, 2, 20operator, 6optional, 22overloading, 11

    PARAMETER, 20parentheses, 6POINTER, 16pointer

    s as dynamic aliases, 19s in expressions and assignments, 17arguments, 18arrays of s, 18functions, 18

    pointer assignment, 16, 17, 19precedence, 6PRIVATE, 21prompt, 23PUBLIC, 21

    range, 2rank, 12, 18REAL, 2recursion, 11

    scope, 21

    25

  • Fortran 90 Tutorial 26

    section, 14shape, 7significant blank, 1special characters, 1statements, 1structure constructor, 3structures, 3subscripts, 4, 19

    targets, 16

    unary operator, 6underscore, 1upper bound, 12USE, 21

    vector multiply, 15vector subscript, 14

    WHERE, 13

    zero-length strings, 5zero-sized arrays, 12