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 !"#$%'&)($0' 1'23457698 . 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
Q RT GU%VWXY S Rc% T deSf7g%ch 4`"ba QSc 66 (can have up to 31 characters, including a ). ip4 7q HH7rts!7uwv4#" (x)y0)((01 E CA
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:
7w%f e7e fbfShdS itu%v`"ba%4`"! 8ft vivi4pddH`"bu 7 u ` ) i Q 1 S i b R 0 g f h f t iQ1hiSR % ` v b " ! a ! 0 1 ) 0 fefh )#)#ititu u % v`"b a! 0i itituu%wv#v#"b a% 4#7"! Su iQ A itu%v`"ba% 6 Su%j)#Q iQ A H#"bau 0k(( B A 6 itu%v#iS"b % a # 4 ! " S u " w a u A igu%v#"baw 6A 7u%)#Q iSR A H#"b 0 8ft vAivi R a u 4#"bu%v`"s!7u%v4#" A t i u % # v b " % a # 4 ! " S u # H " a u i Q # H " a u S i R # H b " a u hSlmS%%e7fbpd7 ituwv#"ba%4#"!7u
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:
itu%7uw47n B B #"!pdH B itu%v#"a 3 3 q HSo 4#"ba B B a leading on the continued line is also required. ed7fbg7%h tfh%rSh7q 'R h7lld h7l
Automatic conversion of source form for existing programs can be carried out by options are: 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 .
(#ts (7u%#" 6 q
ed7ph7q%f
4#" q H7u n
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:
tfh%r7h7q
Q & TR XWX QS for the default kind; but we may also dene, for instance for a desired range of to , a named constant, say uP%4 ou%Hi : gfh%r7hSq 1 f7qw u hfhSq uP%4 ou%Hi hch%e7fh7l tf t7l ) U 0 that allows us to dene constants of the form U SuP%4 ou%Hi &% S Q R T QS7uP%4 ou%H%i Here, uP%4 ou%Hi is the kind type parameter; it can also be a default integer literal constant, like &%QSRT U R but use of an explicit literal constant would be non-portable. t7l function supplies the value of a kind type parameter: The tt7 7ll )#)#Qw Qq%p 70 u P% 4 ou%Hiw0 % S r h and the the actual decimal range (so the user must make the actual mapping to bytes): qw%r7h )`QS7uP%4function ou%H%i%supplies 0 l%f statements, binary, octal and hexadecimal constants may be used: Also, in gd 33 QSQpU% 3 S Q Q V W X 3 RT 3 3 QS7QSn% q%hc ldpg%chf7q%h%e% Sd7 ). We might There are at least two real kinds the default, and one with greater precision (this replaces specify gfh%r7hSq 1 f7qw u hfhSq 4#"ba hch%eSfh7l qwhc t7l ) 1 0 for at least 9 decimal digits of precision and a range of to , allowing X Q 4`"ba Also, we have the intrinsic functions t7 le)#Q S Xd7 4`"b a! 0 4`"bab0 fqw Sq% % h X # ) Q %r7h )`Q X 4#"ba!0 that give in turn the kind type value, the actual precision (here at least 9), and the actual range (here at least 99). ed u fch
Examples are:
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.
)`Q1T X 4#"ba!0
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)
Fortran 90 Tutorial
The forms of literal constants for the two non-numeric data types are:
e w7q%%e7fh7q 3 t i u w # v b " a 9`33 x 9 "!479 urtsI47 H7u% H 9!3 33 (the last being a null string). Other kinds are allowed, especially for support of non-European languages: R 3 3 g7l function: and again the kind value is given by the t7l ) 3 e 3 0 c%drSe7c
c h uts!H 4#"! H v7u t 7 l and the t7l ) f7q%h function 0 operates as expected: We can specify scalar variables corresponding to the ve intrinsic types: t f h% rSh7qt7) l g7l Rb0kv q% h c ) h 4#"ba!0k# sb%H#"bu ec% ddurf c Sw eS cfh7q ch f % q6 ee % 7 % q 7 e 6 b 0 w P 7 4 %7q%we7fh7q )) ch R g 7 l R 1 `"!svb0 $#"!sv7P%47 6 t7l parameter species a non-default kind, and the ch where the optional g7l and ch speciers are optional and the following works just as well: e %7q%we7fh7q )R1 #"Isvb0 $`"!sv7Pw47 6
Here, there may also be different kinds (to allow for packing into bits):
' H#"
For derived-data types we must rst dene the form of the type:
qualier:
Fortran 90 Tutorial
Denitions may refer to a previously dened type:
fefq% h ht! 4 v`"bu 1 o c h efh t!4v#"bu f7 elfhff u % v)Gt! #"b av#"bu! H 0k1 1 e f h 4 h7l fefh uwv#"ba H and for a variable of type uwv#"ba H , as in fefh ) u%v#"a Hb0u we then have components of type tI4v#"bu : uA uA uA
uAA
We note that the qualier was chosen rather than because of ambiguity difculties. Arrays are considered to be variables in their own right. Given
uAAo uAA
HSu%
wH
(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,
and are scalars. The subscripts may be any scalar integer expression. Sections are
$$ 0 d0 0
Whole arrays and array sections are array-valued objects. Array-valued constants (constructors) are available:
U 1 V (b0 Q 1 R 1 T 1 )5) v )51 ( v Q1R Q11Tm1h Rb 01v(b0 Q 1 QSb0 (b0 b ( ! 0 )5)5 1 QSv'v 1 vQ1h QS Qb1 0 Qp(b b00 (b0 ld making use of the implied- loop notation familiar from I/O lists. A derived data type may, of course, contain array components: fefq% h hu % v`1 t l!H7 u h pd7 )5Tb0 q H7u%H c u h fh uwv#t H7l! f7 elfh f) e u%v#t H7u!0!1 u u h Sd7 )#Q%0 u
so that
Fortran 90 Tutorial
There are some other interesting character extensions. Just as a substring as in
8 HS%4& H`"bau igu%v#"ba %e#%Sqf7q ) n%4S e iSH7uI0 t 7 lt 7 l% h )#iQ1 ipR1 g%%e Gf7q%h 0 7 e ph7q!te )) n% H 47 n%47474# "! 47nn iS iSHS HSu! u!00
Fortran 90 Tutorial
%H R #v "buwH7a%H7 %H Q
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:
D D ( E E
In the case of scalar characters, two old restrictions are lifted. Given
V 0 % H 5 i s u 5 ) T %Hi5s u)5T T0
% T0 % H 5 i s u # ) Q Hi5s u)5T R0
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
fefhtf igu % v#h7"b a % h S r q # H b " a u e % 7 % q w 7 e f 7 h q Y q ) "ab0 s!H h 7 l f e f h t i u % # v e% Sq% fhSq 7bQ1 7%R 1 7 % f ef h %)#e7 ituwv#"ba!0iigubQ1 itu%R1 igu% TT
we can write
ititu w TT u w ituw S% TT
t iitu QS ( (4#it"I u% R7u iguR u Q S%R(( 7 SbQ %T ' H%i ) d d 7 7u uw %vv u%vpd
88 p 66 H7n% dp j s t i u v`v`"! Hy 4#4#t! HS % 7 u% v 4`4`"" 6 d j s t i u H ! " y H ! t S H % 7 % u v 88 `v "bu%v#"iSvy4#tIH7%7u%4S4#" o dpj s itu 6 H7n%v`"!Hyiipv7at"dH`"bu qq HSu%47bQ qq HSu%47bQ%0 H H S Su% u%47 47% %RR
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
4#"!7u
qq HSu%47%T HSu%47%T
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).
Fortran 90 Tutorial
For the moment, here is an example of an interface for string concatenation
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.
cf h h igu%v#"baw7uott!H u dtl% fe itu % v#h7" a H`"bau et% f w h 7 r q 7f qwh%e7fh7q ) ch Y 0 igu%v#"baw 6 7u% htS m l f e itu%v`"b ar7 u hf ) 0 fh7q% w 7 e y h S lf wh7 cq% hy f7 q de7h7lqwh 7u%4iSiiSv7at" 1 iS7uw4%iiSv7at" u dpg htS l % 7 e h fh7q% w wc e7h d7q fhSdq% f% dpqqwh )5((b0 p d l y h 7 f 7 e 7 h l u gfh7q%%e7h itu%v`"ba%4`"!7u h S l edSfbt
g q dt% fbth 7u%4%iS fihip v7fat" ) dp)#%i f 1 0 b0 f e f h t ` ) t i u % # v " ! a ! 0 1 e% Sq%%e7fhSq ) ch 'b01 tfhf ) t 0 A 6 7cu%h ii A igu % # v b " w a H#"b adpu %fbth 7)5u% b40 iSiiSv7at" h 7lgqdt g q % fbth iS7u%4 fihip v7fat" ) t)5 0 1i%0 f e f h t ` ) t i u % # v " ! a ! 0 1 e %Sq%%Ae7fhSq ) ch 'b01 tfh f ) dp%f 0 A 6 7l i gitq u% v#" a% h 7uw ju%)#4Q i H`i"b a7 uag" 0 h p d % f t S i S S i 7 v 7%we7fbSd7 itu%v#"a%4#"I7u)#iQ1iSRb0 eSfbSd7 h 7 l 7 % % igu aw h7l u dpl%ch itu%v#"b a%% Sv#u"b ott! H 4#"!7u
i i
Dened operators such as these are required for the expressions that are allowed too in structure constructors (see chapter 1):
igubQ itu%v#"a)5R1 7 bQS(( 7wRb0 8 itugs!7utsbwHy4#"jiguts!7uw47 q% h cc 1 l! u h S Sd7 #) VQS1Rb0 q 1 1 q% h ! l h 7 d ) 0 c%drSe71 c u n 17 Pa)`QS1 R0
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
can write
88 P 4 H7%7o iiSv7at"dH#"bu 6 v q viSv4`" #" 6 iiSv7ag"dH#q"bu ( P 4 H 7 % 7 o 8 Q PP 44 H 7 % 7 oo i6iSv7 at " d H#"bu% u 47nipiS7 7q s! H s!H 8 q 6 PP H 7 % 7 7 v % u v ` 4 m " 4 88 7%7o 6 v q vipv4#" 1 `" 6 66 v7u%v4`"mu%4miSH7uwv4#" V ( q `j)#Q V 1 V 0 6 iiSvSat"dH#"u nj)`7Q a Y 1 V Qpb0 )5R 1 V S P 4 H 7 % 7 o % H 7 % u v ` 4 ! " % u % H t i u # " Y V Q b8 0 7 )#Q iSH71 u%v Q 4#" Rb60 6 v7uwv4#" #" 6 %iiSv7at"%dH#"bu % 7 o 8 4q7 q )R V 0 yq )#Q U 0 H #tt!v#"aiSH7u%v4#" iiSv7at"dH#"bu e7hb tcb g% rf h % f 7 d h h e77 q% chh f
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:
7q% c%w ddp qfbSd7 u dpl%c%d )5 ip4yv#"bu%HSa%H7!0 7 e qqhf f% ef% tw r hf fwet%r h S d g , 7g , etc., but not ch ), these are array valued for array arguments (i.e. are elemental). Like all FORTRAN 77 functions (
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:
e7 h e h 7 chw h e7f ) vSat&" Q%0 ! " S i e7 h )50 e7 "! h iS)#vSQ at " 0 vSath%"e7f h7l "!hiSc
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
ld 4#v#s % u 7 H 8 4#" omv`"bu%H7a%HSbi ""!H7 l d v s1$ 1 t ) 0 e7ewe7ch h tf t ) v#""!HS0 4#sbu%H7 h 7 l l d h7lld 4#su%H7
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
lu%47 d u v d 1" u%l 47ud u%47u j)vb0 h7l u%47u u )kj) d "0y0
becomes simply
Fortran 90 Tutorial
gq% qhdt% cfbth 1o4#sbu%H7 ed7f gtq dt%fbth q%hc o v#" "!H7 o Q h v#""!HS h7l h7lgqdpg %q fbdpt% fb h t4#sb u%HS
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:
%e cf h h v#"bu%HS q 7%v7u dH7u%v u dplf v#"b uwH747qP%H7 1 stt!HS % q h c ht7 l fe f he7h q fw v#" u% H7q% dpq ) 0 fh7qw % S d f 7 h hy h7l u dttl % fhScq% fS %q e7d h e7h7lq%h 66 v#"buwH7 q i ed77f % %te7fSd7 66 v#q "buwH7 q t fi!h)5f 1 g0 f e f h u% H7 q 0! 1 66 v#"u%H7) q 0 i 1 fefh )5)5v#v#" " % u 7 H h 0 AA 4tP%H7 AA 47PwH7 i AA 47P%HS 6 q 6 # v b " w u 7 H i 6 % sttIH7 i stt!8 HS7 eSv#"b H7 q 66 i s S t ! t 7 H %we7fbSd7 d#" 6 Su%47o h7l 76% fbuw Sd7 q # v b " % u S H i h7l u dplwch v#"bu%H7 q S%v7u dwH7u%v
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:
gq dt% fbrSth7qh i tsb 6 i 66 iw0 n n H G ) ! " 7 1 7 tt f % h f h f t 88 `v "tsbu q s!Hi 7 i fh%rSh7q 11 tfhf )) dp%f0 0!1 l! "I h S 7 d 6 6 u ) "!7 i0 7 i `4 sbuttsbu q s!Hi
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:
gq% qhdt% fbtd7 h fd q H 1 ituw7u!0 # v ! " ` 4 " G ) " 1 n 1 1 s t ! t 7 H 1 S 4 % P 7 H 1 S H t r ! s S v % u v H i 1 v # ! " 7 H t r I s v 7 % u v H i 1 # 4 " c b f S 7 d c ! l h S S d 1 u stt!HS 1 47P%H7 1 allows us to call dv#"!4#" by e7cc dv#"I4#")G" 1 n 1 1stt!H7I0 and in dv#"!4#" we have someting like: t ) fSq%h hf ) 47PwH7!00 f wh 8 u%Hitun%4Sit%HiSH#"IH47n 7uts! 7 ats%dH#"bu
Optional and keyword arguments are handled by explicit interfaces, that is with internal or module procedures or with interface blocks.
Interface blocks
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:
h7l
q%hc7%we7fbSd7 dv#"!vpdpsd )51 1 nts"!b0 8 %H7ugsb Hv#"bu%HS q )1 0 q s!H t"jq%i huc Hi d ` v ! " p v S d s d 7 4 n u H t n s I " 7 % u v # 4 " t n s ! " j ) 0 # v " u t f h f 1 %e7h )v#"0 1 tfq% h7qw hq% c 7 % %e7f Sf d7h ntf s"!tj ) 0 h c t 1 ) 0 hStlm Sq% % %% e7fb pd7 nts"I hq% 7 l f S h 7 e h hc n1 n nts"!) 0 8 v#" q 47u%v4#" 47nu H sjipH7nts"!7uwv4#" h7l 7%%eSfbSd7 dv#"!vpdSs%d f%dgfh7q and f7qr7hf arguments (see later article), a An explicit interface is obligatory for: optional and keyword arguments, f%d%tfh7q function result (later) and new-style array arguments and array functions (later). It allows full checks at compile time
Fortran 90 Tutorial
11
tf7h7% qw% % e7hSd7aw 88 a%H#"IH7%v "!pdH pd d eq% 7f itaw p d d j ) l 0 q%hc t i5itt! H d v7n% v1 "!tdHyn%47 7 4 P tb%HviSv4`" h c h c w h 7 e f 7 h S l W ) ) 0 y 0 % a t d h 7l%e7fSd7 7% 8 5i t!Hv7n%v "!tdHyn%47 v7a b 6 aw p d d j ) 0 t wHviSv4#" % q h c h c w h 7 e f 7 h l % q h c t S l 6 ) ) p Q b R 0 0 a % t d d 1 h7l h7l tfhSq%%e7h
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. We have already seen the use of interface blocks for dened operators and assignment (see Part 2).
Recursion
Indirect recursion is useful for multi-dimensional integration. To calculate
we might have
q%h%ep8h q tphyS%%e7fbpd7 v#"buwH7a%7uwHj) n 1 4#s" 6 i%0 4#s" 6 i!)#Q%0huw4 4#s" 6 iI)5Rb0 "b uwv#H7"b auw %H7 7a uw% Hi7n ) 0 n w p 4 d q% h c uwH tfh7 w q % 7 e h 7w%q% e7h fbScdS n) 0 nfbp1 d7 n h S m l S % % 7 e h7 fhS q% % e7hpd7 tfhf t q% hlc t1 l! h u )5Rb0I1 ) 0 4`s" 6 i h7l 7%%eSfbSd7 v`"bu%H7aw7u%H
7%%e7fhSd7 no ) o!0 8 6 s H nts"!4#"u%v#"ji nts"!7u%v4`"mn d 4 q%hc ntsn"! o o1 o onoq i0 h7l v#"buwH7a%7uwHj) n 1 g 4#s" 6 %
q%h%ep q fh%tp hy S%%e7fbpd7 n%Su%47%v )G"0 q%h wcf ) %H%i%0 tt S r 7 h q )G" h %Q%H0 if 1 %"h hc % h H%i Q %H% i "!'Sn%7u%4S%v ) "!&Q%0 h 7 l t h7l q%h wcf clause and termination test. Here, we note the
Fortran 90 Tutorial
12
5. Array handling
Array 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, in
ld v Q1 " i )5vb0 (j )5)5vb 0 )5v 10 vb&0 j)5v Q " 1 vb0 ' )5vb0 v Q " 0 5 ) v Q " h7lld
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
)#Q b0 T
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
gq% qhdt% fbtl! h ith 0 % P # t ) 1 c p 7 d P%7 14 7$ 8 7%7o 7wH7u%H 6 4#" itu%p$ q%hc 11 l! uu h pd7 )) p0 7h )5b0S0 P% 7 4 7 $ P%477$ x h7l gqdp%fbth itP%#t
Fortran 90 Tutorial
ALLOCATABLE and ALLOCATE
13
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
cf h h%P% u dpl% 47q7$"7%So q% t S r 7 h h c w1 cl! u h pd7 ) 1 1 0!1 cc%de7f7g%ch P%477$ h 7 l p d l h f7qdrpuqwh uidv`" P%47)57 $1 'b 70k%" So q% h 7 l ' cc%deSfh ) Pw477$ )G" 1R'`" 1T'#"0!1 ff itu%7utsi%0 l% hccwde7fh ) P%477$ 0
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,
l%hccwde7fh )51 0
We will meet this later, in the context of pointers. Elemental operations and assignments We have already met whole array assignments and operations:
q%hc 1 l! u h S d7 #) QS8 b0 1 6 %itu C H HpdH#"buw iipv7at"dH`"bu igru)50 8 v#S ib "u %v#7"j iS vw 4 nts"!7u%v4`" wHi5s ui7%7o4 sH7u f7qdq% rpq% H%ituu h Sd7 )5Tb0 )( Q 1R 1 T b hcu1 u%l! )(R 1R 1R (0!1 ( ! 0 1 f7q!tn )5' 11 0 f edSf b% teSfbSd7 q% 7% 6 0 6 n 5 ) 1 h c ! l h S 7 d 0 7h )5b 10 n q%hc 11 l! uu h Sd7 ))8 S 0 6 e7fbSd7 )547 iS4pdHd47%H sjiSH7ngs nts! 66 0 nh7l 7 % ' w " 7 w u v # 4 " 7 4 n # " hSlmfSqdrpq% uuwHitu n
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):
WHERE Often, we need to mask an assignment. This we can do using the WHERE, either as a statement:
%h7q%h )5( b 0 Q ( 8 q 4 v 6y6 v q viSv4#" o %h7q%h 5 b0 ) ( Q ( x h7l %h7qwh %h7q%h )5( b0 h7q% Q h ( hc h % h7 % h7l qw h r7h )5b0
(note: test is element-by-element, not on whole array), or as a construct (all arrays of same shape):
Fortran 90 Tutorial
14
Array elements
Simple case: given we can reference a single element of as, for instance,
qwhc 1 ! l u h S dS #) QS 1 S Q b0 )#Q1hQ%0 . For a derived data type like j fefq% h hu % v`t H7u c s6 s % q h c ! l h p 7 d 1 u 5 ) b T 0 h7l fefh uwv#t H7u
u%7 )G" 1 Rb0 u%7 )G" 1 Rb0 A 6 s u%7 )G" 1 Rb0 A 6 s )5R0
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.
as in
qwhc j)#QS 1 Qpb0 "0 5 ) v h 1 Q )#)5v Q 1 d 1 0 sb 0 Tb 0 5 ) v h 1 Q " )5)m v)51h QSQ1 Q X &1Q%T 0 1Rm(b0!1hQ%0 m ( XQQ Rb0 # ) Q 1 R ) 1hQ 0
88 ! t! 7 uu 47 nn 4#4#"I H w 47 P sdS" t 7 7 4 I " H 4 88 P 4 Hw47P 6 H HpdH#" H q47P7 H o u 7 v um 4Snw47P 88 % q 6 v # " % H 7 H b S i H 7 4 7 H iS7%v#tu 7 w u 4 7 m 5 i s 88 qQH 6 Q v i 7 H % a % i ! " 7 4 u % S H % n 7 H % # H I " H %#"$ u Pw4 ipH7u%v4`" dp%f or t%dpwf
Note that a vector subscript with duplicate values cannot appear on the left-hand side of an assignment as it would be ambiguous. Thus,
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:
dummy argu-
u%7 A 6 s
Fortran 90 Tutorial
We note that a given value in an array can be referenced both as an element and as a section:
15
depending on the circumstances or requirements. By qualifying objects of derived type, we obtain elements or sections depending on the rule stated earlier:
As A u% 7 u%7)`Q1 Q%0 s
88 7 %t! 7o4#"!iS H7 uw47 vn4#" #" )#igu 4tH#"b dSt! 4tdS H`"b u 7t s! %7 7ugo sbH%Hy Hpd u 4#"!H`"bu!0
Array reduction
ff gs!Hv7n yq sIHi7%Hyuts!H h ) e ) E 00 f wh t q gs! H 7 v n # b " o I s H % v i i u t ! s H t S d t H s% H74SqnmutsIHH HpdH#"ubi v#" 7w7o d u% v`"!vp dS s dd q s! H v#v#"" ##"" 7 % S oo u% p v S d s ! s H 7 % S f w4 6 s!7u47n 7%7o H HpdH#"bui s%d 4SnmS%7o H HtdH#"bubi
Array inquiry
c %7o 47uwv4#"ituw7utsji 6 vtdH#"jiSv4#" 4#s" 6 i47n #"xS%7o 4SP% 7 H #t! Hy"47s% n d #"H7 7 % SoH Ht)d 47H#"bub iSi v#"S!#0 " 7%7o f 4Su% 7 4 n tt!H7 6 vtdH#"jiSv4#" 4#s" 6 i47n #"xS%7o
Array construction
6 H7ydi#$ u% % a h H s " fq HS p$ v#" Su%7Hy % 7ow7v#o "u%o4#"66 7 % 7o 647n % `"iS$v4#" ` H t 7 # v " a t v d # H j " "t!p$ `"x7w7o47n %#"$ 4#"!Hv#"u%4#" 7%7o s" 6 7 H ydi#$ q H%i #t!H#" 7%7o v7n u hfe "vS% i i v7n 6 #&s47nnSm u w#"ji5t!4%iSHy47n #" 7w7o 47nm%`"$ uP%4 cc 47u%v4`" 7 47u%v4`" 47 4 n nn% n%v7 v7b bigigu ud dv#"!vp vpdS dSs% s%dd
Array reshape
Array manipulation
Array location
uu t cw cwd dee
Fortran 90 Tutorial
16
6. Pointers
Basics
Pointers are variables with the
f%dgfh7q attribute; they are not a distinct data type (and so no pointer arithmetic is possible): q%hc 1 f%dtfh7q q 7 ccwde7fh ) q 7 !0
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):
q 7 q 7 R T the value of the target of q 7 is used and modied. Pointers cannot be transferred via I/Othe statement 7qItfh '1 q 7 writes the value of the target of q 7 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: q% dq t ffh7q 4 sH7u q%h h cc 11 E f% f7 r7h qq 7 E 4 sH7u u%7a%H7u%4 s 8 tI4v#"bu%HSiiSvSat"dH#"u 7 u%Sa%H7u%4 s
but they are strongly typed:
8 v7 H a% &uogt!Hi dSsjituydSu%
and, similarly, for arrays the ranks as well as the type must agree. A pointer can be a component of a derived data type:
fefq% h hH#"b 8 u o s!H u ott!Hyn%47 i5t!SbiSHid7uwv c q t f h% r7h7q v#" 6 H f%dgfh7q f e f h 8 "I7 ! u ! o ! 0 1 " H u 4 u%Hy%H#sbiSv4#" h7l fefh )5H#" H#"uo
fefh )H#"buoI0!1 f%dgfh7q v#" AA q sIH # v " 6H # v " # v " A v#" "!H u
After suitable allocations and denitions, the rst two entries could be addressed as
uu AA qv#" 6 sI H H u A "!H u
but we would normally dene additional pointers to point at, for instance, the rst and current entries in the list.
Fortran 90 Tutorial
17
Association
A pointers association status is one of
lw hc cb cc% dee7fh )Gt 1 ! 88 n%47%H7utst"!v#"ba itu%3 47%Sa%H 3 r 0 S% t )Gt 1 r!0 n 47 iSH7uuwv#"bau%4 "s % l%hccwde7fh on its target without 7%ccbge ing any other Some care has to be taken not to leave a pointer dangling by use of pointer referring to it. detfh7l can test the association status of a dened pointer: The intrinsic function g ) de%tfh7l )Gt!4v#"u%HpI00 f %h
or between a dened pointer and a dened target (which may, itself, be a pointer):
o g e q% cc 1 f 7 q rSh fh7q `) QSb0 )#QS 1 QSb0!1 j # ) S Q 5 1 S Q b ! 0 1 # ) S Q b ! 0 h 1 ! i ` ) S Q 1 q%th h % f d t f fhwr71 h7q dSs u j) 1 0!1 ) 0!1 o) 0 l d dSts u Q1R f %h )o dSs E u Q%0 8 "!4 6 7u%id4 q HpdH#"u E Ey hc h E 8 "!4 6 7u%id4 q HpdH#"u o E i E h7l t 8 4pdd4#" #s 7uwv4#" o ld u f u %c )5 1 0 h7l
For objects of derived data type we have to distinguish between pointer and normal assignment. In
AA q s!H # sbwH#"bu AA q s!H n% 7 v b g i u 6Hy E # sbwH#"bu A v`" 6 H n% 7 v b g i u # v " A n%v7bigu "!H u # sbwH#"bu "IH u
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 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.
If the dummy argument is not a pointer, it becomes associated with the target of the actual argument:
q%hc 1 f%d%tfh7q j) 1 0 cc%d eSfh )5) Y 1 Y b00 e7cc i5s )5b0 gh gq p d % b f q%hc j) i51 s 0)5b0
Pointer functions
Function results may also have the function, as in
f%dtfh7q
attribute; this is useful if the result size depends on calculations performed in the
q%hhc 6 7u% #" 6w H7 Qp bf0 h7q o) 0 q%hc 1 f%)#d% t E o 4pdpt!7u) 0 where the module 6 7uw #" 6% H7 contains 7%%q% e7h fScd7 f% d% 4tdStt! h7 7qu ) 0 4tdSt!7u ) 0 f 1 hc ) 0 8 tb%q% 6 sb 4 H h% tf rSh7%qH "u%4y%Hpd4 q 8 H 6 st v 7u%Hikn%4pd u HS%7o 6 u H "sd H747n 6 vitu%v`"!7u q s!Hi1 " ` v " cc%deSfh )54pdSt!Su )G" 0Stb0 ou H 6 vituwv#"!7u q s!Hiv#"u%44pdSt!7u 8 e ` 4 h7l 7%%eSfbSd7 4pdSt!Su
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
8 v7 H aw
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:
Fortran 90 Tutorial
19
where the storage for the rows can be allocated by, for instance,
ld deS v cc% Q1f"h ) u )5vb0 A )#Q vb00 8 4 7u%Hi%4SPmv 47n H#"bau v h7lld i u
i!)5vb0 A E u )5vb0 A
q%hc 1 l! u h pd7 ) 1 0!1 f%d%tfh7q P%v`" 6 47P 6 E ! P%v#" 4SP uw Hj) d " 1 t r!0 The subscripts of window are Q "!&td Q , Q r%&#t Q . Similarly, for u%S A s
(as dened in chapter 5, page 15), we can use, say,
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 .
sp$w s$
(`ts (7uw#" 6 q
ht!tpch%e7h
#tt a n
Fortran 90 Tutorial
20
7. Specication Statements
This part completes what we have learned so far about specication 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 be forced by inserting the statement
u fcbSetf%dSh
PARAMETER attribute
f 7q% u hfh7q attribute and the constant values to a type statement: q% u h Sd7 f)STbq%0! 1 f 7fq% q u hfh7q n%vH 76 )( 1 Q 1 R (0 fh e fch )1 ul! h 7 h %v`t H7u!0I1 u u uwv#t H7u ) 1y)5( 1 1 (b0 0 l f % fe f h ) u%v`t H7u!0 ubQ 11yu%)5(m R 1hQ 1R (b0m0g(1 u%R A ! l% f ( u S Q 7 ( u % ` v t 7 H u ) s ( l% f 7 W U 7 W U %7 Q ( g 03 (QS%QS'QS l%f S v 1h so 1)`$ (Q 3 1 d 3 XX3 1 3 nn 3 (
DATA statement
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:
Characters
There are many variations on the way character arrays may be specied. Among the shortest and longest are
ee % V 0t'R " ) pd %S Sq% q%% %e7 e7f fhS hSqq ! t SHl ) U 1 l u h S dS ) U 1 V 0 ! $ #"!sv1 c h R b0!1 ! " pdH
Initialization expressions
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 :
lwf f 7q% u hfh7q q hfhf hch%e7fhSl tf t w 7l fSq! u hch%e7fh7l q%hc `7l q%h %fh f7qw hSq tfh%r7h7q 1 fSq% u hfh7q 4#"a hch%e7fhSl q%hc tSl #) QgRb0!17%7o )5Tb0 )5( Q 1R1Tm(b0 g dp%fbgh i!) 1 d 1hb0 qwq h 6 h SdS d1 4 l! h c u ) 1 0 qwgh c ! l h S S d g t d % 7 l V 1 hSq u ) ) 1 Q%0 0 f % h 7 r ee % 7 q% % eS f h7 qq ) c h 'b0 ch % 7 % q % S e f 7 h c h qwhc ) hch%) e7fh7l dq%hc tt)b l 0)50R' f7q%hwe Sd7 ) b000
Specication expressions
It is possible to specify details of variables using any non-constant, scalar, integer expression that may also include inquiry function references:
88 4#i"is u%d v#H "j i i #t!H 7w7o 6 % & 8 # sbuw4pd7u%vy7%So 88 iisdH 6 & H#"bau sH7u #sb w u p 4 d 7 % u v y 4 8 tb %HviSv4`" 47n uPwvHyu 7u 47n
Fortran 90 Tutorial
21
8 6 HSn%#s u
8 iSHSubi 6 H7n%#s un%47yd4 6 s H #4 " o%7u v%i fSg%cbSe f7, q!the fSg%cbSe and its For a derived data type there are three possibilities: the type and its components are all type is 7 f ! q t p f h g p f h 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): chtpd u dpl% v#"! H f7 ! q f h fefq% h h1 f7 g%cbSe vigu c 1itou!0I1 f%dtfh7q "!H u f e f h ) v h h v%itu f7 elfh f) ef vitu!0 u%HH h7l u dplwch dv#"IH
The statement form has to be used to limit access to operators, and can also be used to change the overall default:
USE statement
To gain access to entities in a module, we use the the same as a local one:
Fortran 90 Tutorial
22
8. Intrinsic Procedures
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:
e7cclwfh 7l fb u h ) b fuh! u0
1. 2. 3. 4.
and many have optional arguments. They are grouped into four categories: 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.
e fhw u e7c%de
Sg )5b0
Bit inquiry
sd H7 47n v7ubiv#" u i H d4 6 H gc v7uu%Higu% v#"b a 7 l 7 4 % a v v7u7u%v4#" ge v7uHSH u% h H7u sjv7iSuv q H dp qq p d q vsj iSv Hi v7nu ce "! 47 % a v7nu % ` s 7 m i c v7 47a%v 4pdSt HpdH#"u
Bit manipulation
Transfer function, as in
Subroutines
de u%v#" 6 7u%H#" 6 (47yu%vtdH v7ubi 6 6 v H i q 4#H7t! uts ti"j ik tj iSiS H#sHH 647%#" 4pd "sd HSbi % H i % u m 4 H%ii u%4mitobitu%Htd 4p$
Fortran 90 Tutorial
23
9. Input/Output
Non-advancing input/output
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:
30 4`4`"" ! o! o 00
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:
et% 7q% w e7qfh7q )Tb0 $H7o f % h S r 7 h vS1 u 31h) iSTb v 0 H 3 1 7lwp%e7h 3 %d3 1 p q%h7l )Gs"Isv7"! 7h S u i v H1 h%dtq WW 0$H7o 8 $ 6 k i ! " 7 4 u # v " # 4 ! " y H % H S 4 WH7 W o $v 3 3 H7o)#ipv H Q 0
3 % u S H i I " H u b t % p v d H " s d 7 H q7q! % h7tlfh 5))5'' 11 33 )) QS0 b3 01 3 70 l%pwe7h 3 wdtb3 %0 vpd3 hH"b #"sd H7 edit descriptor:
For entities of derived types, the programmer must elaborate a format for the ultimate components:
There are two new descriptors for real numbers: h engineering, multiple-of-three exponent: RQ X && E RQ X h &T ) h w R 0 h scientic, leading nonzero digit: RQ X && E R Q XSh &R ) h Rb0 b r h c and the edit descriptor is generalized to all intrinsic types ( ( , , , ).
fefhtfith% uw v#"b a H#"bau S r 7 h q e% 7 q% w e7fh7q ) ch Rb0 Pw47 6 h 7 l f e f h itua!%0 v#"ba u%H u fq% e f h ` ) t i u % # v " h7l )'1 3 ) R1 0 3 0 u%H u
Fortran 90 Tutorial
24
d fh and tpI#q%h statements there are new speciers: 7 f% de7fbS tf Sd7 33 q% h7 l 33 3G3 q% hb tS l 3 3 33Gq% f f hS lq!t3 fh 3 l% %h S d 7 ! q t f h h 7 l S 3 f%d f7qd7f wh 3i3 pd7fh 3 3 %d7h 3 b c f7l u 3 eh 3 3 %d3 t p!`q%h there are also and on the q% h7l 7q!tfh 00 3 eh 3 3 %d3 3G% %d 3 q%h7l Sq!tfh 0
On the
New speciers
tp!`q%h ) S dSch%r7f m H`"bau 0 v7u%Hpd!Q 1 vSu%HpdR1 qwh%e7c , or to check that a list is not too long. It is in the same processor-dependent units as qwh%e7c and this is useful to set
is a portability aid.
and thus
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