Professional Documents
Culture Documents
Fortran 90 Tutorial: Michael Metcalf
Fortran 90 Tutorial: Michael Metcalf
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
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:
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
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:
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
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).
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)
for the default kind; but we may also dene, for instance for a desired range of
to
). 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
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):
where the optional parameter species a non-default kind, and the and speciers are optional and the following works just as well:
For derived-data types we must rst dene the form of the type:
qualier:
#"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
Ha 4 %S A #s%o
and the
l 7g
(the last being a null string). Other kinds are allowed, especially for support of non-European languages: function:
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
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,
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
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
Fortran 90 Tutorial
loop notation familiar from I/O lists. A derived data type may, of course, contain array compo, as in : : 4
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
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
Fortran 90 Tutorial
Fortran 90 Tutorial
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:
In the case of scalar characters, two old restrictions are lifted. Given
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).
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
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
E E ( D D
u s%h0 Y ) 77w%7% e iH qhfeq T4u T%74%uSH %7%SH qq T T wtui %TS Twtui wtui
u 4 7!#"
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
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
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:
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
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
0T R 1 X Q SQ 1 X 1 R 1 #Q) h 7e
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
Fortran 90 Tutorial
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:
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
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:
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
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
Arguments
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
#"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:
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
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
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
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
Fortran 90 Tutorial
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
$ " !&%# !
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.
Fortran 90 Tutorial
Array elements
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
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
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
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):
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:
and, similarly, for arrays the ranks as well as the type must agree. A pointer can be a component of a derived data type:
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.
#"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
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
attribute; they are not a distinct data type (and so no pointer arithmetic is possible):
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
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.
or between a dened pointer and a dened target (which may, itself, be a pointer):
Fortran 90 Tutorial
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
u H #"%b`s gub7%n iv
For objects of derived data type we have to distinguish between pointer and normal assignment. In
Association
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
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
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
where the storage for the rows can be allocated by, for instance,
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
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 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
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
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
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
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
Fortran 90 Tutorial
21
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:
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
$ 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
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. .
edc h %7e wu f e
Subroutines
Transfer function, as in
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
& 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
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
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
h h d
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
Fortran 90 Tutorial
On the
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