You are on page 1of 28

Fortran 90 Tutorial

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

1 2 3 4 5 6 7 8 9

Language Elements Expressions and Assignments Control Statements Program Units and Procedures Array handling Pointers Specication Statements Intrinsic Procedures Input/Output Index

1 6 8 9 12 16 20 22 23 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 77only 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 assistance in preparing this tutorial. Version of October 1995

8 6  3 2  0 ( & %       9754'1 '$)'$#"! 

Fortran 90 Tutorial

1. Language Elements
The basic components of the Fortran language are its character set. The members are: the letters and the numerals ; the underscore and the special characters

From these components, we build the tokens that have a syntactic meaning to the compiler. There are six classes of token: Label: Constant: Keyword: Operator: Name: Separator:

(can have up to 31 characters, including a ).

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

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

signicant blank handling; indentation; replaced by ; name added to subprogram statement; and etc. syntax converted.

The source code of the program can be obtained by anonymous ftp to tory is and the le name is .

$ $ %s %ps

fqhpd %77e

a leading on the continued line is also required. Automatic conversion of source form for existing programs can be carried out by options are:

u 4  a v d  fa e l u a R u a Q 7!#"a%b#"wu atui 47pb7%v%SmSh v u s v 4 biu#"Hv iA Si f  #"uH a A i R )u #"Hu A uS!v#"%bR#"%tui ( #"4%u7!`"%bQu #") av v u 0 Q b#"H Si  #Q%7 6 wb#"%gui Si B (k0 ab#"H A i  #Qj%S 6 %b`"t%8 ti A i S7#"u A6w#"#"w%ttiuuvv i!!%#itu %) Ab#"%ietuf u ! 4 %ab i A 0Sb `"v#"4 a vf a a h u H d d 4 i v i Ri 8 1 Q  0 i Q f ) 4 1 0%ab v% dh fef b`"pv tShi  bS0R t1 )`i7!gfhu `"!!b`"`"%tutui#i) SSb7%w7 a v fe f

0 7H P A 0!7I54G")) 3 2 1 0 ) F( 'E &D %C$ "# !B 9@8  6   n 7H q #" u 4 1 0 () 0 ) (  AC   E  #"!t7y7xi( 4 u H 4 vw7 s r H q p 6 6 a cgfedcc b"`4 h%7S%TR  Y X W V TRS %GU SQQ l7h dl l 7h B a v #"%tui a o B b#"4 SH q 3 3 p!#" BB Hd n4uu B 7w7%ti u s 6 #"%7(  #t( f%7h7e q q p d R ' qhrhf 7S%t h fd %7gb7e

   

(which are equivalent outside a character context);

(CERN Program Library Q904). Its

(130.246.8.23). The direc-

Fortran 90 Tutorial

Fortran has ve intrinsic data types. For each there is a corresponding form of literal constant. For the three numeric intrinsic types they are:

that allows us to dene constants of the form

Here,

is the kind type parameter; it can also be a default integer literal constant, like

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

and the

function supplies the actual decimal range (so the user must make the actual mapping to bytes):

Also, in

statements, binary, octal and hexadecimal constants may be used:

There are at least two real kinds the default, and one with greater precision (this replaces specify for at least 9 decimal digits of precision and a range of Also, we have the intrinsic functions

to

, allowing

that give in turn the kind type value, the actual precision (here at least 9), and the actual range (here at least 99).


This data type is built of two integer or real components:

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

Number of signicant digits Almost negligible compared to one (real) Largest number Maximum model exponent (real) Minimum model exponent (real) Decimal precision (real, complex) Base of the model Decimal exponent range Smallest postive number (real)

d ehqf hcgd 7S %%%7%pl

for the default kind; but we may also dene, for instance for a desired range of

to

, a named constant, say

). We might

iHu 4P %o  %u

n %7SQ 3 QQ%3V QSQ 3 XW TRQ 3SpU S 33 dg 0iHu 4 hr %%%o  %7S%`QfuP l ) 7%wq 0 i H u h rP w%o  4S%%q  %7w#Q#Q)) 7t 0u p 7t ll l 7t R TRQ  S%& iHu 4 %o %UP u u  u u i H u io 4o S4 P T7RSQQ %%%H %P %US%& 0 U ) 7t  t  l7f7%hh  i%uo  %u  Sh u w7f 1 S7%g H 4P  qhf q qhrhf l f h e c  XW R  SQ  X T  &  Q
        

hc d f u e hr 0a 0a bb`"4 !b#"4  X `Q) 7%wq 0 e7 q !bXa `"#Q4 ) 7SXd #Q)%t%h Sf l a b`"4  X Q 0  1  ) 7t  wq  7S%h  b#"4  Sh u w7f 1 S7%g  l ch lhfehc a  qhf q qhrhf

0a !b#"4  X `Q) T1

0 )e 0 ) gbf r 0 ) 7h %%q l 0 ) 7 !hS%q e q 0 ) pd d f %w7uf fh h 0 ) Sd% hg u f h 0S%)f hr 0 ) 7f d 0 ) 7%tftcp!hl r

qhrhf 77%t
Examples are: :
      

ch %q


Fortran 90 Tutorial
The forms of literal constants for the two non-numeric data types are:

Here, there may also be different kinds (to allow for packing into bits):

We can specify scalar variables corresponding to the ve intrinsic types:

where the optional parameter species a non-default kind, and the and speciers are optional and the following works just as well:
 

specier replaces the

For derived-data types we must rst dene the form of the type:

and then create structures of that type:

To select components of a derived type, we use the

qualier:

and the form of a literal constant of a derived type is shown by:

which is known as a structure constructor.

#"H '

 c h

Pvs 0vs 1 qhfeq 6 74w7!`"$ bI#" R) 77w%7% e h l l 7t c 7g P v 0 s l 6 74%7s!#"$ bv!"` 7 0 1  R h ) q h f 6 7wg4 P bR  cc ) 77ew%q7% e h q7hf7e q c wc%7%d e u H6q b b#"%% f hSefc ru d% Sec #s k0!a vk0bRb#"47g 7)l t7S)h %t%h q l q r hf 0 %7 f ) 7t hq l l u7v  H 74t H  !#" !tsu h c

0 V 1 3 7d 3 ) #"SbS!t  #s%o TR uv 4iH 4

#"SibS!t f 7h 4 H hfe l H t7 0 Hw!"S#Q) 4 e c7 da q fSi%7H h hq f e 7#"h 7%!wt%ef q

H 4o 0 4iH hfe d 1 #s%h`"Sb7! )t f

Ha 4 %S A #s%o

 

and the

function operates as expected:

and again the kind value is given by the


l 7g

cerd 7S%c 0 3 e 3 ) 7t l  3 3 R 3 3 Hu4 4 3 !9 %7Itsur 9x 3 a 7Hv 7!" 3 9b#"wtui `3 9 qhfeq 77%%7w e


    

(the last being a null string). Other kinds are allowed, especially for support of non-European languages: function:

form. The explicit

n4 u s S Si 0 H7u7#"H t t d S 7 o b%bgsb!I!igu4#" Sp)4 mS%Si #" 88

H u 0b ) %7H q A b5R5Ruu 0 )
so that

u  %#Q) 7dS h u !l !!7H #t% )u f 0 10u v hfe Hu  0 u v f %7H q bT5) 7H #twu h e l d H vcuhf hq f7e 7p h 7uu !l `t1 %%fh

d l 0 b( 0  0bpQ 00 Q QT  v SQ 5) 1 ( 1 1  v ' 0 0 b( bSQ 1 Q  bb( 1!bShQQ v 5(15)) 0 (v0 bm1 R 1 ) 0bR 1 U 1 T 1 v R 1 b(h1 V  1 Q )5v1 Q 0 0d  0

u#tv%7Si u ab#"H 4 %SHH 8 u4$ 45si% 7%7%q 8 H $ !#"P 4u `"`"% 88

0s' ) b5vj

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

 

#td  0 & 1   d ) 7h r h f  b V   V SQ  5) 7p h u !bl0 p1 #QjS%t q %q ch

u %SH

AAu oAAu

H w

Au Au Au u 4 b#"vIt u 0 a v hfe bH #"% )u f H ab#"wu v fe 1  kb!#"0a bw!Gt) vf 7h 1 H uv#"v 4u h hfef l ubH v ab#"%feu f h h fl e o#"4!t cf 7h u1 v 4 h hq f e b`"!t %f

Denitions may refer to a previously dened type:

making use of the impliednents:

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

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

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

which, in turn, have ultimate components of type

we then have components of type

and for a variable of type

Fortran 90 Tutorial

loop notation familiar from I/O lists. A derived data type may, of course, contain array compo, as in : : 4

5() (5(5)) 5(5())

0 ) $ 1 R  5T)j 15v)0 #td )j  $ s  s5v) 5v#tjd 0 ) %#Qj

AAu

0 uSH n7 4 e !SHSi 74 7% )n t q h u n !l7p 0 0!SiGf  %4%g !#"1H p4R i 7%)nQ41 #i) %7et h% 7 e q h7 0 u H 4 q q % I7Si e S% )n 7fS%#t7l e
 

uq f h !7f u f h h%q c!q q #e fSf7%%c l 7


 

a v ua &4 b#"%gui b`"H %SH 8

ab v )0 a H#"%a 0 d 10b H qSh! e  q %7Iigu t  5s  i 8b W ) 7S h bu0 v! l 5v!b5sj)Y) %77tf w%7% e
 

There are some other interesting character extensions. Just as a substring as in

Finally, there are some new intrinsic character functions:

0Q& )0 )Ha %v  5vbbsj%7!t

Also, zero-length strings are allowed:

d 0  Y ) HVpU RA bv  5v)bR30 #Qj%I"T S#sQ %34 o XW

was already possible, so now are the substrings

Fortran 90 Tutorial

Fortran 90 Tutorial

2. Expressions and Assignments


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

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 for assignment. For scalar relational operations, there is a set of new, alternative operators:

so we can write expressions such as

In the case of scalar characters, two old restrictions are lifted. Given

it is now legal to write

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

we can write

For the rst three cases, assignment applies on a component-by-component basis (but can be overridden), and the rst two cases require us to dene the exact meaning of the // symbol. We see here the use both of an intrinsic symbol and of a named operator, . A difference is that, for an intrinsic operator token, the usual precedence rules apply, whereas for named operators their precedence is the highest as a unary operator or the lowest as a binary one. In

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

a#"v%ti " 74b#"t7S!" u s n H uP H d " a v i i 4 6 w74 #t 7H q 4 88

a 7 v vHd 4 a ini ! v%7 q %7vas " S4% 88 h 0 ( l   HH ! %7 qi%74iS%nn % f s  mv 7   D  5) 7 tn

u o b"`#"4vSt%7I#tH!`"S76#"%uijs`"pvs d 4H d a H 4 v u u u" 47upv i%i7H4yHy%vn iH v tib `"`"%7%S!#t#ty!`"`"%7HH 66 tjpdd 4v%u7%SH! 4yH! vv n6 utijsp

R4u q Q4u R%74%uS q 0 Qb74%uS i H d v vwu7 %7%SHH  %b7%SHH qq %'p%u %7d )d vu

T T7 R u 1 Q Q i 0 a v u %%gui 1%ti 1 bgui!b#"wt#i) 1 R%7 b7 a v u S7ff %%%ef q hh ee hSe q fl H 0 #"%ti f 7h u!as q b ) q ha eh%%q % b#"H Y 77#"f7w%i7tef bq Srhguf h f e v

88

4 bQS ( QR S 88 Rugi %7%I#"t(Stutii T u7R u i( (%Qu

 E E  (   D D 


Q %  7%7wb#"v  R % H HaHu H 0 ) iH 0 ) iH% TR  #Q5Tuu 5s5s%


u s%h0 Y ) 77w%7% e iH qhfeq T4u T%74%uSH  %7%SH qq  T T wtui  %TS Twtui  wtui
     

0 ) iH% ) iH 0VT  5T5Tuu 5s5s%

u 4 7!#"

h c g 7 g  r e f h rSd f f %twf w% h hq tS%fdwd%c7 7b7%q d e pef q 0HaHu v 4 dcl !7%S%b#"ypi 5) %%pd u q


, , etc., but not

t 0 U  #Q) y 0 V  R) q q u H d " a v ui#"H d " v i i #"4 v 4 vu H i #"a%u vH o 7 b#"%t7Sb%6t7#"a Sw7#"u v%67S#"7!Si#t 7%Hq74 88  i 6 4v ) 0 ) u#"H4#"d twSm4%um`"%7vu !`"%7bR%H 4iQ7%1 Y 7 )#Q Ho  4  P b80 SQ  V 1   0 5R  )bp` Q  V 1  Y  7`Qa jn " vSv7iiSi 6 #" 4tv%u% 4 v u 0  vV o auH uiH 6 V 1 V  #Qj Q q ( V PP 46u 6 `" 1 #"pv q v 6 7%7 8 q H H d7 a7v i ii n %umH`"4vta7vv i6 i6 o7%7 !s !bsu #"q Hg"7SSpi74b#"%7S 7%7HH 44 PP 8 q d"u o ( 6 #"b#"StSqi 6 7%7HH 44 PP 88    u H`"4v" i7vv iv o7%7 8 d a o

0 1 )a R S`Q7 n  d 11  0  R 1  7dS 1 P q  b0S#QV )) 7S

4us i 4 Hsu u 0R ((Q 1 ) a v Q 7w7!tguj#"ywbt7!gsti 8 bw7 Sb7 5R#"%tui  bgui

 

 

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

h u 4 Hwat u% v du i f ec%7l l lh 7!#"!b#"o S%bigua #"%7StbS%%pd u77h t v 0Ri 1Q ) u 4 bS#i7I#"  a v u  f d %#"%ti 7Sb7w%7 dfe h e q h f tu bl Se 0 g"7S`"i i #Q %jSw7 %%%tiqu l ua a 7vb iH 4 ) u Sui h tf#"vpd g a u 4a v 7!`"%b`"%0 u (i h 7%%7gSh A i 7h bt5() wq l  f A f  1 0 6 qdf7h% dh q f h ch f l p%7qe f7dywpdq h l i  0 %d )0 tf ) "tb' !!c#") %`i7)f %%ef a qu q  0%ip h fth f 1  0h v Sht e hSf%e hSe7hq eg7ufS 7%%77w%th "avii4u "aviii t1" 75S)v 7pSb)57%7Si td b%t t7S%w7Si 1 t7SS%04 7u ) whu7S7yw%d7t a i i tav ii0 u  4 u  h q f d q g i %  4 f hq 7r7e d q fyh7ewpq h f lh hcl  f ua l chh tb%pbHg#" A i7h u a v  0 a v hfef l %7 6 wb#"%gui   Y  b`"%tui mSh 1 b' %7 ) 6 wb#"%gui i 0 u a v u a q h q f hb ) q ha f7e7rwh7 A c `"H 77%w%te f e i  0 )  0 f h i f t i 1  0h#"v Sht e hSf%e  bt 0 1 %#if )fpd) t7pivfah" tS%!!c%`i7)f %%ef %a7 q tb%tq 4 u hu f d q g H o u a#"v#"v h hcf !tt7wb%%itu gui %tld u
 

h hf l u7I#"4%"#% a v q l h q 7wqe %h7cq lt 7h 0u (i h qdf7Se7df7dyhe%pq hu f bt() %p%w7S%%d7t h f

For the moment, here is an example of an interface for string concatenation

Like all FORTRAN 77 functions (


The order of expression evaluation is not specied in order to allow for optimization on parallel and vector machines. Of course, any operators for arrays of derived type must be dened. There are some new real intrinsic functions that are useful for numeric computations:

c e c r dh u 71 S%c ! ch% hh u !ll 1 %qq

can write

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

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

Fortran 90 Tutorial

), these are array valued for array arguments (i.e. are elemental). 7

hc h7e f rfh 7dtc7q h %gb%b7eh % fh




Fortran 90 Tutorial

3. Control Statements
The CASE construct is a replcement for the computed GOTO, but is better structured and does not require the use of statement labels:

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

A default is available:

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

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

becomes simply

 H q 4  !s y 8 iH q  mo #"4 8 P H a H u v74 H t i H7 q hyu 7%7%#"!Hto u !4s 7%g 7 88 n q ibSH%7H%b"`mo #"4 8 a u v

Hu dl l H " 7%#s4 7h u 7H%bs#4 S!#"v ft h 0 ) t7h d l  l hce 7we7e 0 ) t 1 $s 1   v l d l d

0T R 1 X Q  SQ 1 X 1 R 1 #Q) h 7e 

0bvj u du l l )  74 47h "%u 1 d 7%vu u 4 d   7%ul

0 0 ) ) y"  d jku  7%u u4

Q %& 0 ds !SH  )"

f avc ih  l h Q  7%t"e SS!" 7h  0 a #Q)  t" 5S)v !h " 7e 0S i   %t"Q &Sa S)v !h " 7e 0 i h 77whh7e e fehc fchl %7%yh 7e


H" 7H!u#"v  7%#s4

Fortran 90 Tutorial

4. Program Units and Procedures


In order to discuss this topic we need some denitions. In logical terms, an executable program consists of one main program and zero or more subprograms (or procedures) - these do something. Subprograms are either functions or subroutines, which are 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 the statement function:

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), whereas y 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 the statement that contains them. Modules are used to package

global data (replaces COMMON and BLOCK DATA); type denitions (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 detion, interface block and function subprogram is:

and the simple statement

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

vuH uv Hu hcl l %7wd 7%S q 7%b#"v wpd u 7h o 4 7%uS i H u v 6 d u e l 6 #"d 7dSbf7w%7 H 8 q S%b#" 6 7HSbSvf %%67 7 h e S!  H t t H t t SH%74tP ts i 7HIs  7!Ss i q 7wb#" 6 AA i 7i wP74 7AA %#"7 %t6P6H 4h0AA i q 7%wb5v#"u #") h 6 H u v q H7H v 6f u  1  0 q  g0 )  5f1 !ih) t q !701 wbu #"q7 %#"5v) def f H v H6 u6 hfe7 7Sef % f d f t%7 7e i H 6 h heqhf l q 7wub#"v 6 0  ) %q l h 7%%St u 7h qdf77e7dfSdyh7e% q h f pw%HuS%wdtl 7t qhqf hc fe H t %H h chf l S!t s q 1 77%7#"P 4v %q 7h v u H u v HSHb h hcf l %7d 7%7 q q7w%bv#"u #"v %pd u u fe

o4u h fdqg 7w7 6 #"d gb%p 8

vuH uv Hu S%7d 7%7 q S%b#"v h

Hv u f d q SH!S"" #%bh#s4 h% fg d g l l tbtpb%pq 7h 7h Q   o 7!"#"v h o c%td% g H tbqq f d f h t 7e c h H u o h f d%qq g 7%b#s41 tb%t


iH i!Hs u ustu stt  0 !s q q bbb`"`sv4 88 i 6 7  i 6 7! )"

d 1 f f h f q h r h f 7S h u !l !0 %pd ) hft 1 7Sr%ht 0 7 1 ) ) f nn h %f q g wii 66 7I" i  6 70 !tG" Hbts i 1 7tqh Sb%ttdf

Arguments may be optional:

Any reference to an internal or module subprogram is through an interface that is explicit (that is, the compiler can see all the details). 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, specications and statement of the procedure concerned, either placed in a module or inserted directly:

0 uu 1 iHvu sr b  7%ti   %Sv !tSH 1 1 1 G") #"I#"d 7e n 4 v cc


u Hdsa su n4 H HiHt 4n uiH b#"%t7 !t7 7I#"S%iS%t%u 8 w f !7w74 ) h %Sf ) t h 00HP f hq #"!#"d 4 v 0Htts I7!1 1 n 1 G"#"I#"d 7e ) 4 v cc #"!#"d 4 v v c h 0uu !7wti 1 H q #"%7v It7!#"v %Sv !7t%H74 1 71 %S4!1t s 7!H Sst S1d 1 h !1 l G"1) `"c !S#"d b7t1b%t 4 1 i H v u s r H 1 i H v u s HSP H PSH t t r nu 47 fd f hd f d%qq g

Interface blocks

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

An explicit interface is obligatory for: optional and keyword arguments, and arguments (see later article), a function result (later) and new-style array arguments and array functions (later). It allows full checks at compile time between actual and dummy arguments.

qhf d 7t%%f fhrq qhf d 77f 7g%f dsdv v dfe l %Sp!#"d 7SbS%%7 7h 4vu"sn Hi u n 4vu )"  #"w7!t7pjs H 74 #"%74 q #"v 8 0 !tsn  n 1 n %h q c "s heqhf l Itn 7%%St 7h  ) 0 s fn e l 0 jt! )" th 7pd b71f %%ShmSh df tf e c7cq hq 7S7%%he%%hq f 1 0 h 7%w7t 0 1 Hu v ) " s 4 v u s  #"v) d d v 1 h q  ) q S%b#"H u #"v 0 j!tn #"%7Ib!tHs n 1  7!H541n ) ps p!#"dfS!`"f tv iH c u%jt"bgs7% 8 0 " tn u ds d vq vs p7Sb7df w%7i%q u H " d e ch

l 7h

Arguments may be keyword rather than positional (which come rst):


and in

allows us to call

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

Fortran 90 Tutorial

We may specify the intent of dummy arguments:

Arguments

we have someting like: by

10

fc h w %q l 7h 0 Q & v 4 u n ' H l %! )" %S%7%S!"  %ti % 7h Q  %%h h iH c h 0 % f %Q h G") t 0 i H f c h 0 v 4 u 1 f i%H% q hS h f e h %%% ) w %q G") %7%S%n 7p"d b7%Sy7tp%r tq p%%q e h
clause and termination test.

0 " %i 6 #s4 g 1 n jw7%7wb#"v  n 7h )HuaHu o l o 1 o %oq o n c hq nm"`v%7!tn j"#%"#!tn H s 4 4 u "s i v u 4 "s 6 d 8 0 " d h f e ! )o !nts7n S7%%7 o

HuaHu %7w7%b`"v d f e l 7SbS%%7  7h i 6 `s4   0 f h f 1 0 d "  t ) t Ib5R) 7p u h 1 c h h f h q !fl %q n 7pb%%%SmSlth 7h d 7e7e%S l 0 1 n n SScd f%eq ) h hbe7%w7h f H a 7%w7cht Huq 0b5R)Ii 6 4 " d 4 w7nu %7wba#"Hv %q %04s# i wh#s%40 #Q !1) i n6 jw#sH") 7% 7wbn#"v 0 7b7w7%%S7tpbu " h8q p%%q "u u4 apHwu ) p iH%u yhw e h 6 d fe

0 " o )HuaHu Hd wi 6 #s4  o 1 n j%S%7%#"v  s 4 q

#"4Swbt 7v 7%nyt!" t!a5ti6 8bp )Q St v i v H a 4 H d 1 %7vd H% 0 0 R l vnd `"vS%bt 74 7%nyHHtp" vv7H%ty0 W ) St 4 i v H P 4 d d 1 %7t a i 0 l vndd !!" %I!%i5tH#"H a 88
11

heqhf l 7%%St 7h  c%q 0 7jwha 6 ) 7S%%%h h l ) 7ehpcw dh7eq 77 hfdd cf l  h%q 0 l7jwhha ) %q 7h c ) 7e c c h l hfd dpwdtd a d 7e7%%q h f ipw7S%w77t hfe


Interface blocks provide the mechanism by which we are able to dene generic names for specic procedures:

Here, we note the


Direct recursion is when a procedure calls itself, as in and to integrate f(x, y) over a rectangle we might have

where a given set of specic 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.

Fortran 90 Tutorial

Overloading and generic interfaces

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

Recursion
We have already seen the use of interface blocks for dened operators and assignment (see Part 2).

Fortran 90 Tutorial

12

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

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, in

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

is a valid do nothing statement. Assumed-shape arrays These are an extension and replacement for assumed-size arrays. Given an actual argument like:

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

and this is as if da were dimensioned (11,21). However, we can specify any lower bound and the array maps accordingly. The shape, 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

#t%ti h f7pd%qP g l P $4 tb%7 7h  $ x 4 $ p%uti #"4 6 H%7w7 7%7 8 $771 %P  Sb5) ) u  1 c77%P u H o 4 00 h   7p0 0   ) p)dp #t%thPh iu !ll 1 %qq  h 1 7d ! ch% 7 tb%t h f dqg

l l 0b5v) ' bv 1 "  Q  5vj 0b0v "1  5v)j)v5 (  bv5 ) 0" Q 5v)d 7h 0 ) & Q 0  1  0 i" Q b5v) v d l

0 b5)  5si 7e cc  bR   SQ  5) 7p h u !l 1  %q  0 1 d ch

6  0  1  ) bpd 6 )  h5siu !l 1 %q  07 gb%p h fc d h q g

T  b  #Q) 0

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!).

The work array can be propagated through the whole program via a USE statement in each program unit. We may specify an explicit lower bound and allocate several entities in one statement. To free dead storage we write, for instance,

0 77% )P 7w%l $4 hfedcch 0%tu7%uti  f !"#T1 `"'1 G") 77w )P S%c is f 10 ' R $4 hfedc o " 071 v7 d l h S%kb 5'7) 4i7%q ' $ `"%P u h qu r d q l wp7f $ 4  77%P  hc f d c 1 %g77e%c !0  1  1  ) 7dp h u !w1ch d%q 7h h q l c p f h l o " $ 74SrP h h l S%777%%%tc pd u

We have already met whole array assignments and operations:

Elemental operations and assignments

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

In the second assignment, an intrinsic function returns an array-valued result for an array-valued argument. We can write arrayvalued functions ourselves (they require an explicit interface):

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


0 b5) hq h h r hwq7h% l c 7%7% h 7hh 0 ( ( h q h b  5Q )  %7% h q h  l w7%x 7h 0 ( ( h q h b  5Q )  %7%  o  #"Sv q v y6 4 q 8  Q  b   5) %7% 4vi 6 v ( 0 ( hqh

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

!( 1 1 ()   !b( T 1 1 Q ()   b5T) 7S 10 R R R 10 R  0 d

uiHu twu q r d q f l d f e %plSmSh 0 66 "# 74 #"4wu7!"tn gsn7Sjs % n v s Hi H n 7Sb7w6%1'7 7nh n7d4 d0H 1 pbS40 5i ) 754) 8) u 6  h7S0  )0 7S h u !l 1 %q  d ch 67S1d 5) nh !Sld bS%%%q 7 f ec h7f d 1 tbSe 0  15' t!7f ) fn  q i!Hl u 1 c hprq d q h tuu %%u%%7f q

u7Hb`" Ho7t%vp ub#"HHHw H "`u%iv 7u !ts n 4 Sj#"%b#"v us " 7 i7ii wu 5sip 4 " v i v u 4d a d 0 )u 5gri   C t% 6 w  1 7  SbS8 8#Q) 7S h u !l 1  %q  0i d c h 0  5) 7w%l 1 hfedcch

WHERE

We will meet this later, in the context of pointers.

Fortran 90 Tutorial

ALLOCATABLE and ALLOCATE 13

a %7H v 8 fd f wp%t %pd


or dummy argu-

s 6 A 7%u

is illegal. Also, a section with a vector subscript must not be supplied as an actual argument to an ment. Arrays of arrays are not allowed:

0 T 1R 1 b( U 1 hQ 5()  b( X 1 X m5(m)  0 0 1T 1Q )

Note that a vector subscript with duplicate values cannot appear on the left-hand side of an assignment as it would be ambiguous. Thus,

4vuH 4P $ QR ) H H n u 4 `"%7pi wu #"% 0 0 X  h11)  ) 6 HI"#%7%SH%7! i a i4 v Q 8 u" v% %7iH uQ 8 0 Q 1 0 ( R 1 bR  1Q Q  Q#Q) #t%7Si  5sm7w7H q 8 %h!bm%1QT0 X m5(1 ) Q Qh1m) P 4 n 4 7u H74dpH H iH q H P 4 7wSmH6#"Sb76H 7v% u #"vo 7% 8 b0& T  Q  "  Sh5v5v) 7H q H 8 " d P 4HI P 4 n7 7! Ss 7wI#"#"447w744 H u47!Ptt 8 4 H n u 0 s 1 ) b01 d #Q5v 88 0  0S ) Q 1 ) w bpQ 1 #Q" jh5vq  c h
      $ " !&%# ! 

 

0 0 5R) s 6 A bR 1 G") 7%u s 6 A bR 1 G") 7%u 0 0 bR 1 G") 7%u

 0 1 d 10u v hfe 7wu  R S`Q) 7S h u !l I!7H `t% )u f


u v 7H #twu f e 6ss  b5T) 7p h u !l 1 %q 7h  0 d h chf l c u v h hq f e 7H `t%u%f 0Q 1 ) %h#Qj  bSQ 1 S#Q) SS h u !l 1 wq  0  d ch


as, for instance,

as in and a reference like

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

is an array of type real, and

is an element (a scalar!) of type triplet, but

we can declare an array of that type:

Simple case: given we can reference a single element of

Fortran 90 Tutorial

Array elements

Array subobjects (sections)


. For a derived data type like 14

The general form of subscript for an array section is

o v H o7w7x vHI 7w7x`"`"`"`"Iss

dsd vd ivn n 4vu qq %Spv!#"gugub7%744 `"`"%744 cc d%sSdpv d ib7v%nn7 4v%u7

edc edwc wt u


4P $ n o n4 Hi4 i %u "`%m74 7w7 #" 7yS%!5tj#"w f u u7n i i n n & 6 v7v mS7#s4 %Sv" eh n o H i 7%7 #"!#t %H q $#y7H 6 s o7%7"#v #"%#"v!"#4 #"% n77w7x`" p!" i d " 4 4 ud H $ 4 o $ t i$Sj%tv746 7%7v"# 66#"%u iv7wod %y %S"u s Hv apS`t HH q `"#"Hn oa 4o  o 77 H $ #$#"7yH76 #"h%%uf o7%7%Sx#" v 7i H6 d#s4  n#"Sj#"dtvs 6 7!ut o7 n 4 u " 4 v i H d H t 0#" "#n ibb#"tH H 74 7H  %" %S4 f o ! 7Sii o 47 d n 46 Ho7 P 7%SxS#" 4 s u674) i4 S%vv7iu #"H t7y!%#tH S ji t7wt#su"  #"#"4 wSj#" 4v 7%4 c

h q fd%ftf g d7 I!%%ehf
 

hf h w %q


ldg 7whp h f7S l c%g l h f 7ewdpdc7 77%c




iSb H i d uH HH d o o 74Smn 4% %#"bpb#"tH7v%HH 77%7!4sSds v6 wvs f n ud d ou 7 #" #"v!s q dSp!`"%u o o iu7 H H#"Hts u nq 4 d d s 7w7 #"vS%b#"p#"d H I!mS7SHs pv %%u Hs h 0 e t d s Hut Hiiv HI q b n7vH! w f 0  E ) ) t  H St !Hh t!y%7%Iss qy#" 7!gsgs ff s u iH o nv H


u f c l d q 7ep7uf c p pt u f d %epe cc


u i o H 4 & v%u R n 4#t%uu 4 v t uS bS%7yI#"#"`"4$ w7yv 7S!sp6s d %%iS4%ul v

u u f e l d c7 fd 7p%f  7l q f 0 1 ) 7% As %Q `Qs 7%uu A 0 0 1 h1 )j %Q %Q #Q#Qj Q )

0 u H 4 u S HpH H H u7%t7 47vu Hi 47St47 !b`"!#"!bt#d" y%bgs7!gu#i) #" #"w7bS`"H !#"!% 88 t 4d os n4u otd 0b!#"4 4w 4 7 H o H $ u b#"0$ w7) H #"w`"v %) Si 77%S7i 88

Array location Array manipulation Array reshape

l hf7 7ew%f% q h r e7 S%huf q


Array construction Array inquiry Array reduction

Arrays intrinsic functions


depending on the circumstances or requirements. By qualifying objects of derived type, we obtain elements or sections depending on the rule stated earlier:

Fortran 90 Tutorial

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

Fortran 90 Tutorial

16

6. Pointers
Basics
Pointers are variables with the

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

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

the value of the target of

writes the value of the target of 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:

but they are strongly typed:

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

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

After suitable allocations and denitions, the rst two entries could be addressed as

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

ud ui s iH ou a %SytjSd !gt& %7H v 8

#"Sb#s%y%7I" 8 u !"  7g%f 4vi H Hu4 H  qhf d vud Hi 4n Hto w7iSbS!5ti 7%y!tu 8

u Hd"avii Hu v4 #"tSSS%b#"It 8

u !" A u H H IHs 6 #"vq AA uu

v HH!" A #"v H! v !"" AA #"#"

s  %u7%HS%u E  7 q 4uHa s  %u H a  7s  4  7 q 4 fhq E ch u7%H  q hf7rt7d ch% 7s7%4 u  7%ff 11 %qq 7 q 1 hfq 7 q ' tI7 7 q T R  7  7 q q

  u  q h f7 q d%u q h r h f7 7 q %b#"v  7t%f#"1 v 7E7%tq

#"  7g%f !Ib#"H) f v  qhf d 10ou hfe

q  qhfd ch 7  7t%f 1 %q qhf d 7g%f 0 hfedcc !7 q ) 7w


attribute; they are not a distinct data type (and so no pointer arithmetic is possible):

is used and modied. Pointers cannot be transferred via I/Othe statement

ou hfe l 1 0 o#"H hff 7h H!!H!s#"v uq7#"5H7)h %te f 6 r hf o q u h hq f e b#"c H%f u !" A #"v H v H IHs 6 #"vq AA #"#" v

u I" A ub#"Hwb#s H u H H ub Hwb !Hs 6 `"vq AA b#"#"wb#s#s

u !" guib7v%n H iv E  H H #"v A guib7v%n y!s 6 q AA gub7%n

u H #"%b`s E  gub7%n iv u H uiv  qhfd 10ou hf b#"wb#s 1 tb7%n  7t%f !!b#"5H) e f

h 00uHa Hu v4 lhfe % f !7%S%u 1 p%#"!Gt) 7t%d ) g


 

e cc gb%7

h 00Hu v4 lhfe % f Ip%#"!Gt) 7t%d ) g lhfe 7td h d ch f7ewc%l u a 3 %%7%34s " ti %b#"!tw7%i7%nn 88 !r 1!rGt) 1 Gh t )7%bwl eccc H S u 4 avb"#"vutu7HS 74% 0 0 f edtc%hS a suH 4

Some care has to be taken not to leave a pointer dangling by use of pointer referring to it.

Pointers in expressions and assignments

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

Fortran 90 Tutorial

The intrinsic function

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 , we can write the following code (although, in this case, the same result could be achieved more simply by other means):

#"w7 #s #"p 4vu 4dd4 0 1 5) 8 c f dl l o l % u u 7h E t 7h u Hd #"pH 4 d u 4 i  q i%7 6 !" 8 EE  o c h h yEE  o u Hd #"pH 4 d u 4 q i%7 6 !" 8   h 0 R 1 s d % f %Q  Qu E S )d u tSs d l u Sd q h r h f  s  h f 77wcht 0   ) 1  1 o 1 0 1 S ))  q fhrt7d ch% bS`Q) 1SQ`!ih!0bSQ#) !0b0 S15Q) S!)#Q1 j0 ) !b!0 1Q 1 S #Qj  7S%ff 11 %qq  q e g  o

causes current to overwrite rst and is equivalent to

u H #"%b`s  gub7%n iv

the assignment causes rst to point at current, whereas

For objects of derived data type we have to distinguish between pointer and normal assignment. In


A pointers association status is one of

Association

undened (initial state); associated (after allocation or a pointer assignment); disassociated:

can test the association status of a dened pointer: on its target without ing any other 17

Fortran 90 Tutorial

18

Pointer arguments
If an actual argument is a pointer then, if the dummy argument is also a pointer,

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

Pointer functions

Function results may also have the function, as in

where the module

The result can be used in an expression (but must be associated with a dened target).

Arrays of pointers
These do not exist as such: given

then

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

utd4 u td u iH 7!Sp44%#"v!s q u7!#"wtv 6 H u S!Sp f e l vui b0o `t0 4 8 u t d 4 7Shd bS%d%c7  7h e) fe " i!s q 7!`"v%tv 6 77H  " H SG"S!Sp5) S%c 1 H u ui n4 ds u 6 `"v 8 h o 7%SH d 4 i H u t 4 d H 4 " Hq S h 4 u p%nk%7v s 6 H q p%y%u %7b%6sr ctfH h %bt 8 0  ) 7!tSt4  q h f 0  d ) h%q u d 0 ) 7!tS%f 1 7S7%%7 u7t td4% dc f%eq 7H %6 "# w7 6 u 0 7!pp E  o ) utd4 0  )o  7t%%f 1 %h q q h f  d c 0 p u c h 7H w6 #"b #Q%) 7 6 h%q

contains

a w7H v 8

qhfd 7t%f

) 0 b5)0   5s1 i  jg b% h ch fpd%qq g 0 b5)  5si  7e cc 00 1 ) hf dcc b Y  Y 5) Se % 0  1  j  7t%%f 1  %q )  qhf d ch

0 )iP4  0ou hfe G"Ib7%  Ib#"H) f

u !" A b7% H iP4

it must have same rank, it receives its association status from the actual argument, it returns its nal association status to the actual argument (note: the target may be undened!), it may not have the INTENT attribute (it would be ambiguous), it requires an interface block.

attribute; this is useful if the result size depends on calculations performed in the

Fortran 90 Tutorial

19

and then dening arrays of this data type:

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

The array assignment

is then equivalent to the pointer assignments

for all components.

Pointers as dynamic aliases


Given an array

that is frequently referenced with the xed subscripts

these references may be replaced by

The subscripts of window are

(as dened in chapter 5, page 15), we can use, say,

to point at all the u components of tar, and subscript it as

The subscripts are as those of tar itself. (This replaces yet more of .) The source code of an extended example of the use of pointers to support a data structure can be obtained by anonymous ftp to (130.246.8.23). The directory is and the le name is .

  n a #t t

hehcp 7%t!th

v ua n v P4 Hu 00 0 b#"H 74 mS%i%74 8 bv  #Q) A b5v) )u Q%v 7hl h" f 1 Se d dcl d cl

u s q 6 #"w7(  `t(

s A S%u Q  #t%r  Q Q  t!"  Q & d& 0 !r  t 1 "  d jH ! wu E  S4 6 #"%P ) P v P v  qhfd 1 74 6 `"%P  7t%%f !0  1  ) 7p h u !l 1 c %q d h

0 1  )  fhrq ch SQ S#QjH I %u  7Sf 1 %q

0   7t%%f )  qhfd

0 G"u !G"Ii  !7% ) f ) 10 )  0P4 hfe s A 7%u E  tsS%u 0 1 bR #Q) tsS%u A b5v) u E  A b5v!i 0 0 ) 0 !r  t 1 "  d H ! %u ) u  i

$ $ s wps

hfe l cf 7h P1 4 h hq f e 7%%f

. Similarly, for

 

74 u7 uyw n H vP uu 44 4%vu iHvdp4Hb 7uH s a7n y`" S7wi%ib#s t 8 v du b#"H & 6 s 88 d o o H v u Hdp4 u 7w7S%!t#7 yii&%7vwbi#s4 8 %j6#"%u#"is 88

000 e q ' 0 b ) 7dS wh%7f )05R btl)t   h f q ) q h c  l h7 h cq7hh f e cq7h c %bq0h d ' 7h%)e hfSe%%%wq cc ) 7S%%h7f% ee d 0 0 l  V  %Q 1  ) 7 SdS !S7%chwhg d q h r c 0% td1 g )) SS hh uu !ll 4 11 wqq   0 ) h fd dh q g bh1 d 1  !i g6 b%p


0 b( 0 )  7 vu u 1 0 u % h f e v  1 10 (  R 5(1y1 Q 1 ) H() #tw7 %nu  7h u %7hf u !%bSTq01 ) f 7IS!7Hh u`t! 1)u %fq q    6 H v  q h f 7hq f d l ch qhf q 7h u %7f

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

Implicit typing

This part completes what we have learned so far about specication statements.

PARAMETER attribute

A named constant can be specied directly by adding the

It is possible to specify details of variables using any non-constant, scalar, integer expression that may also include inquiry function references:

and statements, or in specication statements with these attributes, are constant expresThe values used in sions that may include references to: array and structure constructors, elemental intrinsic functions with integer or character arguments and results, and the six transformational functions , , , , and :

0 T 1R 1 b(mQ 5()  b5T) o7%7!bgR#Q) St  ch%q  lSf7%hh  a#"4  7hh u %Sf 1 77%t 0 10 l h e c  q f q qhrhf qh q hf h S w7f % %q l l f h q l f hfe c fh h 7`  ch%q  7h7e%ch u !Sf 7t  t  lS7%hh fwq f wl
  

qhf q 7h u %7f

Hp!" d vs   0 V 1 U ) SS h u !l !bR  c t#"' 0$ d 1 0  h 1 R! V

Q Q  Q  ( $ ) 1o s f 3( nn 3 1 X 3X d 1 3 S S%SQ 3 Ug h1 v f%l 3 H v %7!"`mS%7 74 #"vw7Si o #"4 vuv v o n 4 uH ( R 1 0 0  (' ( )0 7  `Q7%(S f% 6 6H v u v u n u t d 4 H 4 v %Sv!#"R%74b`"H!#"4!Stm!"`o #"4 88 ( !s A %u (gmb( R1 Qh1 m%)R5(y71U W1 bu  u7HW !7`tu Hv%7`ut %Qu )u fll u Q  0 vS hfe% f %l

Specication expressions

Initialization expressions

qhfeq 1  U StHl) p!) " S7%%S% ee d qShf7e%%qS%


  

There are many variations on the way character arrays may be specied. Among the shortest and longest are

hd ec S%tfSbf u

Characters

DATA statement
Fortran 90 Tutorial

at the beginning of each prorgam unit.

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

7. Specication Statements

attribute and the constant values to a type statement: 20

Fortran 90 Tutorial

21

PUBLIC and PRIVATE


These attributes are used in specications in modules to limit the scope of entities. The attribute form is

and the statement form is

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

For a derived data type there are three possibilities: the type and its components are all , the type is and its components (the type only is visible and one can change its details easily), or all of it is (for internal use in the module only):

USE statement

To gain access to entities in a module, we use the the same as a local one:

statement. It has options to resolve name clashes if an imported name is

or to restrict the used entities to a specied set:

These may be combined:

ecg hf q ecg Sb%Sf gp!7f Sb%Sf

H s 6 y7%u #s%7H 6 bSSi 8 4d 4n n iuH

u #s%SH 6 8 n h

0 b')  q q d f q h f 1 6 q du fSr%q h f p%w77d 0 !06 ) )f hp%S77d 11 P 11 q11 s  Sbw!7ff o  h ef cgp7 g H v hcl l I#"d wpd u 7h HH %u  0 i h f e  u !tuvf e ) f u !"  7ht%f I!%v ) f 7h H  q f d 1 0 tituiv h hfef l guiv  eo 1 c g c h  Sb%7f1 %hq f eq f hf H vd c7 l !#"h tp!%fpd u hf q tp!7f i%v u   7%o #"4  Sbgpw!7ff h ef c q7 g

ui tv E  tv  4  7d 1 !#"d h ui ec H v

ui tv E  guv  4 1 !#"d h i H v

P1 q 1 s  h f 7 1 q r  1 o 1  tpIbf%7771h %%htq e pq c g f ch f ui ec H v tv  7d 1 !#"d h

$ dHuio p4 t%tbti 4 i i H im%u % i s d H Hm4ki i " iHs 7 bSH  d" p4 6 #"%746 6S#sH Sj%u jt%tH q it u H d v u 4 i%u7 v! t%y7( 6 #"b7v 6 #"i %u#t4 de Hu Hv

 e d c w h %7e wuu dfl e q h l h  dl7w 7wg7uh7 wu 7wuqq h u bf  7 tf!wl l f7p hg

hehcp n ut iH H 0 S%t!ph 74 S! #t% 8 b1 3 6  3 ) 7 %7f  v  S7%g qh q  qhrhf


 

u#"Hp n t d 4 %7 d va uHu7nvSpii v%a74 c 7v m7v i`s %74v e q H q Sjs !" c qp v i pdd H q Sjs h #"4%u7% 7v H u7H v u u u 7vu SH7v g l Huva e a7 v i %u7 b#" %gu%7v4 cg H 6 iH u #"b7v  74 7H  s 4d v iu n d

0 g b5) S

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

0 l hfl cc !u  h u bf ) h u bf  7  w7e

Fortran 90 Tutorial

We have already met most of the new intrinsic functions in previous parts of this series. Here, we deal only with their general classication and with those that have so far been omitted. All intrinsic procedures can be referenced using keyword arguments:

1. 2. 3. 4.

elemental work on scalars or arrays, e.g. ; inquiry independent of value of argument (which maybe undened), e.g. transformational array argument with array result of different shape, e.g. subroutines, e.g. .

b00 5)1 75 )p w%w7%fhq q d h f e h

The procedures not already introduced are:

edc  h %7e wu f e

Subroutines

Transfer function, as in

e fd % 7w ff%q d %pp fqd hp%gh`g q e 7ctf!`g l7`g f 7wfgg h

Bit manipulation

h 7p  tfIg

Bit inquiry

8. Intrinsic Procedures

; 22

u H% lh uu 0%3H u 0 a  !0v1 R )a #"3%`itu1 ')) fq v h7fe% 0 b#"%tui h f e l 6 74wP bR  u a h e f 7h hb ) q w%q f e c#"H 7b7#"f7w%tuih7%tf h a q vSr h f e
 

and the edit descriptor is generalized to all intrinsic types ( , , , ). For entities of derived types, the programmer must elaborate a format for the ultimate components:

0 bR ) c 0  bR wh ) T  & h h  X

 &( h Q R R Q & R SEh X &

& QR &   QE R X X  

0 q d s H d v 7H "  Hu#"p%b3 t d h e p l 0 bSQ ) 1 5') h l h7 H d s  H d v 3  7H  " p%bt u IiS%b" 3h 0 w 3  7w%7 3 1 3 0 ) 33 1 5') tf7!%q

) o H W 4 H 0 Q H u p4v #i" i7$ o H 6 S%3H3 y!#"4  #"v 7!kv 7W$ 8 o7$0 W  t%h 1H Si  h7p 1 % 3  7e%wl7 1 3 H 0bT ) 1 7IGs) l  H W qd v 3 d h p uv" S7h$ 13 bSTu0 !)v 7sq" 7w%%t%h eq q h q7f ov i h f7eSr%h7 H
 

0 vu 0! l h 4 v%u !oo `"4 lS%q 1 !#"%#t54) 0 `"3 4 3e %q 1i !#"#s4 %SH#t546 )) hS h v u n

New edit descriptors

This shows how to keep the cursor positioned after a prompt:

The next example shows how to read a record three characters at a time, and to take action if there are fewer than three left in the record:

Fortran 90 Tutorial

There are two new descriptors for real numbers:

Normally, records of external, formatted les are positioned at their ends after a read or write operation. This can now be overridden with the additional speciers:

Non-advancing input/output

The rst three new edit descriptors are modelled on the

engineering, multiple-of-three exponent: scientic, leading nonzero digit:

h h d

H H  Sv i4 h  %7H   SSq 3 d h e p pd 7 % 3  7%%%h l

binary, octal, hexadecimal.

9. Input/Output

edit descriptor: 23

ceh 7%wq

ceh 7%wq 1 RdHu QdHu ua p%Sv 1 !p%7v 0 b`"H m 7%SS ) %`q!pt frhcd h


0  h q hq 3 % %G3 d 3d % 3 3 3e 00  tf!Stf7!%qq h h l7l h% 7 h %`q!pt 3 3 h 3e  u c7hf l dd% 3 3 t3f !S3lS%qG3 33 3 33 Stb!%Gq3i3 hw f7q733 f 7l %h% 3 3q 3  7SStfd b7%fl h q hd% l hh t7ff h7 33 d df lh f 7f7h qp be% d S f d% h hf %#qIpt 7d
and there are also

and this is useful to set is a portability aid. and on the

Finally, inquiry by I/O list (unformatted only) is possible:

Fortran 90 Tutorial

On the

New speciers

statements there are new speciers:

, or to check that a list is not too long. It is in the same processor-dependent units as

and thus 24

Index
actual argument, 10, 12, 18 aliases, 19 ALLOCATE, 13 argument, 10 array construction, 15 array constructors, 4 array elements, 14 array inquiry, 15 array location, 15 array manipulation, 15 array reduction, 15 array reshape, 15 array sections, 4 array subobjects, 14 arrays, 4 arrays intrinsic functions, 15 arrays of pointers, 18 assignment, 6, 13 association, 17 assumed-shape arrays, 12 automatic arrays, 12 binary, 2, 20 bit inquiry, 22 bit manipulation, 22 blank, 1 CASE construct, 8 CHARACTER, 3, 20 comments, 1 COMPLEX, 2 components, 3 constant expressions, 20 continuation, 1 conversion, 1 cursor, 23 DATA, 20 dened operators, 7 derived data type, 14, 16, 17, 21 DO construct, 8 dummy argument, 10, 12, 18 edit descriptors, 23 element, 4, 14 elemental, 7 elemental operation, 13 explicit interface, 10 expressions initialization of 20 , specication of 20 , formatted les, 23 generic interfaces, 11 generic names, 11 heap storage, 13 25 hexadecimal, 2, 20 implicit typing, 20 initialization of expressions, 20 input/output new edit descriptors, 23 new speciers, 24 non-advancing , 23 inquiry functions, 2 INTEGER, 2 INTENT, 1, 7, 9 interface, 6 interface block, 10, 11, 18 intrinsic functions, 7, 15, 17, 20, 22 keyword, 22 kind type parameter, 2 letters, 1 linked chain, 16 LOGICAL, 3 lower bound, 12, 13 matrix multiply, 15 model numbers, 2 modules, 7, 9, 10, 21 named constant, 2 named operators, 6 numerals, 1 octal, 2, 20 operator, 6 optional, 22 overloading, 11 PARAMETER, 20 parentheses, 6 POINTER, 16 pointer s as dynamic aliases, 19 s in expressions and assignments, 17 arguments, 18 arrays of s, 18 functions, 18 pointer assignment, 16, 17, 19 precedence, 6 PRIVATE, 21 prompt, 23 PUBLIC, 21 range, 2 rank, 12, 18 REAL, 2 recursion, 11 scope, 21

Fortran 90 Tutorial
section, 14 shape, 7 signicant blank, 1 special characters, 1 statements, 1 structure constructor, 3 structures, 3 subscripts, 4, 19 targets, 16 unary operator, 6 underscore, 1 upper bound, 12 USE, 21 vector multiply, 15 vector subscript, 14 WHERE, 13 zero-length strings, 5 zero-sized arrays, 12

26

You might also like