Sie sind auf Seite 1von 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
Specification 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

(which are equivalent outside a character context);

 &(' )*  +  , - .     ! # " %$ 


/ 10%2
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:

53 46 /789:; 
35?4?6 @A5B%C?D "<>=
  $$ 
up to 31 characters, including a ).
EF" %G 0 0%HJI%KML" T(can - have

 +U)
NOPQRSP

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:

V%WMXBA%eB>fY5D@5X EJKZL<>= ["<YgX[B%DK\X<B E3]YJ^X E54> &_BJ` LaEbLaEcT["FddS0<>K


BefD EJEJKKZZL<L<>>==i)  EJKZL>= [ "[ 5K  E)3]hE54
EJKZLE5>4 =) EgK[Z"L>[=M5K $ %EJKKZM]La3 =M E5$4 )%K 0>=K `E3  EJK&_ZBJL<`>La= Eb$ La5KEcjT3 [ "E>3 K) L< 0I>%=KK `L"k  *
DX5lmEJKV5WZXL>A%=B> YF[@%X"EJ[K5ZMK L) > =0S [="K ` [%K E3 )  0S=K `  E54 )  0>=K `

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

* FdS0  ! EJK%ZKM "%n **


* EJKZLS= !  G 05Zo  ">=
*
a leading on the continued line is also required.

Automatic conversion of source form for existing programs can be carried out by
options are:
significant blank handling;
indentation;
replaced by
;
name added to subprogram
statement; and
etc. syntax converted.

A@%XB>YgX%WD
YJXBDr5D%q 4

tI  %u $ q

A@%XpD%qB (CERN Program Library Q904). Its

DX%lTlS@
DX%l

A@%XpD%qB

[" G 0%ZK  n

The source code of the


program can be obtained by anonymous ftp to
tory is
and the file name is
.

sFZ  [[  Z   [  I (130.246.8.23). The direc-

Fortran 90 Tutorial

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

YJXBDr%D%q

3    64 :9:  35
for the default kind; but we may also define, for instance for a desired range of
to
, a named constant, say K2"  oK0aE :
YgXBDr%D5q  f%qM u DBD5q  K2"  oK0aE  D?DA%BD%l YJXB YJX%l  7 
that allows us to define constants of the form
 35354 %K6 27 " 5K 2o"K 0oE K0aE
Here, K2"  oK0aE is the kind type parameter; it can also be a default integer literal constant, like
3546 7 4
but use of an explicit literal constant would be non-portable.
YJX%l function supplies the value of a kind type parameter:
The
YJYJX%X%ll 3M
q3F %KX2r5D"  oK0aEM
and the
the actual decimal range (so the user must make the actual mapping to bytes):
qMXr%D <35 %K2"function
 oK0Esupplies


lB statements, binary, octal and hexadecimal constants may be used:
Also, in
C@ !! a35 a3F7 a835 a9:\3 !!
 ! a35 %35n46 !
qD?
lS@FWC?DTf%qDAY Y5@%X ). We might
There are at least two real kinds the default, and one with greater precision (this replaces
specify
YgXBDr%D5q  f%qM u DBD5q U ">=  D?DA5BD%l qMD? YJX%l    
for at least 9 decimal digits of precision and a range of
to
, allowing
3 :  "<>=
Also, we have the intrinsic functions
fqM5qYJX%DXlAar%DY3 Y5 :@% X :  "<3 > =:   "<>=>
<3  ">=
that give in turn the kind type value, the actual precision (here at least 9), and the actual range (here at least 99).
A@ u f?D
Examples are:








  

  



<3]U6  :  ">=

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.

lYFraYJB  
Df YJ?@%X
War%D    
u YgXDD ff@5@5XXDDXXBB  
uf%qMDAaY YF@%X  
q5lY    
qXr%D  
B>YgXe  


 







Number of significant 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

A M%qA%BD%q
'<!! N"%EJ' KK `ZML0%>Z =' '! ! !!
HJI1"%K0
(the last being a null string). Other kinds are allowed, especially for support of non-European languages:
4 ! !
YgX%l function:
and again the kind value is given by the
YJX%l  !  AaYY ! 
?@raY5A%?

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


 V? D  
KZJI0 YJX%"l 0  L%K
and the
YJX%l   B%qWD function
  operates as expected:
We can specify scalar variables corresponding to the five intrinsic types:
qAYJXD@ BD?f?r5 D%D qYJX% l YgX% l"> =4>kkL
?A @u%raqY5MA5A%?BD%q ?DX f[I>ZZ ZG$0> K
A %qMA%BD%q  ?DX  44 >  2MYgX%"%lZ $ S<sL> SsL %2"%Z $
YJX%l parameter specifies a non-default kind, and the ?DX 
where the optional
YgX%l and ?DX specifiers
are optional and the following works just as well:
A %qMA%BD%q Q4\ S1sL> S<sL %2M"%Z $

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







For derived-data types we must first define the form of the type:

BefDA Mt%q0%ZSE5A%"BD%q
35 S^%=MJdS0 0

q

D


?
DX%l BefD t05Z>E5"


and then create structures of that type:

BefD  t0%Z>E5"<\ho"I  da0

To select components of a derived type, we use the

o"I ) 5=0

qualifier:

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

o"I  t05Z>E5"  ! dSL%K ` ! _46  8 




which is known as a structure constructor.

specifier replaces the

  0

form. The explicit

Fortran 90 Tutorial

Definitions may refer to a previously defined type:

BefqD Dt"? L<>K


DBX%elfD BefD t "oL>K
KfZD L/t>"=L> K0 k\   [

B

e
DX%l BefD KZML>=  0
and for a variable of type KZML>=  0 , as in
BefD  KZLS=  0>UK
we then have components of type t1"L>K :
K) K) K)[

which, in turn, have ultimate components of type

K))

K))o K))

05K[ 

ZM0  :


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

qYJDXB?Dr5jD%q 3F >l Y DX FY @%X


 u
#  35  \_ 8  8 >   Sd t




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

j3

j#Ls>

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

jdS #tL # sSL   s\


jj#dS6  t4S#L\

&& Z <a "0


&&_ZG <0 a[%K"%KZ 2"E#I  ` E5[%ZLtSK
05Z"  0 >=K

    d] 

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

3]#L U 4\L U6\  3]7   8 h>4> >


T# #  L 3] U4\3]Uh6m35 > > U>L  3  35 > >
#  35L\ L  3] 3F > >
lS@
making use of the implied- loop notation familiar from I/O lists. A derived data type may, of course, contain array components:
BefqD DKZ? L<t l 0%Y K DX YF@%X
DBX%elfD BefD  KZMuLt  0%lK Y D#X6> Y5@%X _G 0%ZK0
 KZLt  0%K u
3  K
##
##
#

so that

K\K\##4>4> ) G 0%ZK0

&&  E5[  5Z Q EgKZJI[%KgI>Z0>


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

Fortran 90 Tutorial

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

A % qMA%BD%q  ; > lY u DX 5Y @%X  9 >&  t1%=0


 t5=0j#s>S#L  L>
E#I  gE KZL>=


was already possible, so now are the substrings

! a35)46 789:; \! #L  L>


o"I 1FdS0j3  4>

Also, zero-length strings are allowed:

t%=0jQs>>#L  La3

&_ 05Z"  0<>=K ` EgKZL>=

Finally, there are some new intrinsic character functions:

%lA W%q B?


q?B%qDDXfY D B5qB Y u
u


%YJYJlX%lAWD 5qB%q  n"5Z  CAaAYY E5/0%B%K1qW D 


ApD%%qXYJVe E n3]"% Z EF"4\ 0 "%n  E505K 
 n"%Zb  "%n  E505K




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:

Z0  4  L>KM0%=0%Z  Z0  3

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

, ,     - - 

so we can write expressions such as

YJV #  ,   X%l  Lm  sS B DX&& n "5ZiIadS0%ZL[ G %ZL  0aE


n  %=    
n"5Z  %" =L[ iG %ZL  0in  %=


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

A %qMA%BD%q  ; hZ0aEQI  K


it is now legal to write

ZZ0a0aE#E#II  K\K\##66  86S

 Z 0aE#I  K\3  6S


Z0aE#I \K #6 4S

&& " G %0 Z  t   "%2M0 $


"Ta EE5L%=Jada0>KT"%n I   JE KZLS=

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

BefDYJXBEgKDZr5LD%>q =
 0> =K `
A

%


q
M

%
A

B
%
D
q
;
G
DA X%l5qBeA%fBDD5q EJKZL S`= >  I` 0 `
BefD EJKZML>=i[EgKZ>%Z>3]3]  [EJKZ%Z4\4   [EgKZ% Z6 6


we can write

EJEJKKZMZM66
[EJ`KZM5Z6 6

 JE KZS35 aEJKZ4 
 [EJ``KZS5Z3 4["1[ `[%%ZK 6 EgKZ4
[ 5Z>3

&& Fd IjEJK $ 0%nL<0O"t05Z%KL"<


&& Fd<L Ij>KEJKZL]$ 0E5$LL<[O"0Ot1"0%tZ05%ZK%"5KZTL""<  o
dFjI EJK $ 0%nL<0OaEEFL%=JadS0<>K

For the first three cases, assignment applies on a component-by-component basis (but can be overridden), and the first two cases
require us to define 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

 ["[%K 

GG 0[5K"%Z6
0[5K"%Z6

 Sd %KZML   
 ad %KZL KLFdS0E

GG 0[5K"%Z>3 RRGG 0 [5K"%Z4


0[5K"%Z>3 0 [5K"%Z4

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

Fortran 90 Tutorial

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

JY XBD%q@FlVW?A5DTDOf%@%qSfD%@qMA5D%lBW@Fqq D Q>
EJKZLS= ["1[%K
DX%l u YJXBD%qVMA%D

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

u @JlWBe?fD D EgKEJKZZL>LS=M= %KoJt0


YJXB%qMDMr%D%A%qBD%q 0<>?=DKX ` ;
A
DYJX5XlmBD%qBeVfMDA%DOEJKZL< >Y5=r%X  DX aB   EgKZL>=M $ %K

u

F
@

l
M
W

?
O
D
%
f
S
q

@
%
A
%
D

l

W
M
q
D
DYJX5XlPBuD%qYgXVBMD%A%qDTV@%fA%D5D qB@Fq [ %K" aE5 aEE5L%=J  E5 %KM" [ EE5L%=J
#EJ>K ZL<>= ["<[%K
F
@

l
M
W

?
O
D
%
f
S
q

@
%
A
%
D

l

W
M
q
D
u
A@5XDBX5>lPYJX YgXBD%qVA%D




WCBqSef@JWD B>YJXD [ %K" E5 YJXaBEDEFXL%B=J @FWE B  [>


A ) 5qA%<BEJKD5Zq L S?=DX  >S YJXBD XB  YJ X  
EE ) EgK0>Z=L>K `=M $ %?KD X  #[>[ 
DWX%lCqS@JWWCB>qaYJ@FXWD B>YJXD [ %K" aE5 aEE5L%=J
BA e5fqD A%<BEJKD5Zq E5LS %?K=D"X  [ YJXaBEDYJEFXXL%BB=JD  XYJ#BX[  ^@FWEB 
D[ X%l E W) CEJqSKZ@FWL SBS=YJX $D %>KMSj 3  E )  0<> =%K `  
V%WXMA%B>Y5@%X EJKZLS= E5 5[K"1" [[% K\aEEE53]L%^=gE54>
DDX%l X%l @FV%lWWX? DA5B>Y5@%X EgKZL>=M ["[%K
u
EJKZL>= 5KoJt0


[E

[E

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

EgKZ>3  EJKZLS=\#4\ [ ` % Z>35[ ` %ZM4> & EJKZgI[%KJI>ZM0O["jEgKZJI[%KM"%Z

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

qqDD??  llYY u DDXX 5YY5@%@%XX  835 \_4 >   \   U[


 G   2
 
?@raY5A% ? u
n % =\<35 \ 4 S



can write

&& 2 `` "  0b%ZZ%o aEE5L%=Jada0>K


[    
&& 22 `` ""  0b0b%%ZZZZ%%oo $aLEGE5LaL%=JE5aLda"<0>KT $"%n aE5E[E5L%=ga%ZdS0G>K  I0
[ cG   
22  8  G 3 j3  8  8 
&& 2%`ZZ" % o0b$%ZL ZG %LaoEFL"$$  L%K< L$"<m K$$" L%KEF[L"<m %KZ "mG E50I[%KM0 L"
n[j <%3 =  ;   8  <3F >  ]#4   8  53 > 2  "   0b3 % Z; Z %oT3 8 Z 04 >% KL"<  K0EJK  $ aEE5L5=JadS0SK
&& % ZZ%o E50[%KL"  $$ L%KML"  $ EE5L%=JdS0>K
G Q4  8  OG 3  7 
" G %0 Z  ttLS=RE50[%KL" aEE5L%=Jada0>K

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

DXA%D>Df%YJq?>@%XDYgXDXBr B
A%?D

VV%q?M@@FA%qB>Y5@%X
u @FlW?@ #  EF"OL>K05=0%Z
qqDB fD AafYJXM@5Xr DXB fMAaYJXr

YgX , %C , etc., but not ?DX ), 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:

%A D ?DMD A%BT  A%a3 D


A% D aE5#L5 S=J  
A% D aE5L53 =J   
DX%l  aDE5?L5=JDA%B 


 Iad  05Z
3





&& %X W u CD%q "%n KoJt0TLSK0%=0%Z


 O G   I0aE  0  "%2
& "  om
&  OG   I0aE   " G 0T

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% D 3  4  :  35  3 :  46S




A default is available:

A% DOlDV%W?B


There is only one evaluation, and only one match.


A simplified but sufficient form of the DO construct is illustrated by

"LISK0%0%ZZ  Sl @ Sl @ L  s\^  
&
YJV  A%eMA%?D "  omL<>K0%=05Z>E
YJV    D SYJB
 L05 Z "I>K0%Z

D
%
X
T
l
S
l
@
DX%lTlS@ "ISK0%Z


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

SlK"%@ K L   d  _
DX%lTKlS"%K@  K"%K  jQL>

becomes simply

K"%K  W \u kj d  \O

Fortran 90 Tutorial

4. Program Units and Procedures


In order to discuss this topic we need some definitions. 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:

W CqqSD@JW?B>YJXD "I>K0%Z
Uo

A@%XBWSCYJqSX @JWB>YJXD
qD? o L 0%Z
o   3
DX%l DX%lWCqaW@FCWqaB>@FYJWXB>D YJ"XI>D KL05Z 05Z


& WCqS@FWB>YgXD da $ %KM"%Zo




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 definitions (themselves a scoping unit);
subprograms (which among other things replaces the use of ENTRY);
interface blocks (another scoping unit, see next article);
namelist groups.

u @FlBWe?fD D L>L>KKM05Z0%GZ G   %ZL%K ` dS0%KL[


DYJX%XlBqD%BqMDeVf? DA%DTL"%S2@5Kf0%D%0%Z qZ  G IBM t@Ftq 05Z
  $$

J
@

l

W

?
O
D
5
f
S
q

@
%
A
%
D

l

W

q
D
 L>KM0%Z G   E
DX%l u YJXBD5qVA%D
A@%XV%B WSXYJXA%BSY5@%X
BBeeffDD #LS $K$0% Z LG > KM 0%Z  G YJ X BED#X\B    Yg X   \ 
$ $$$ L#>LSKMK0%Z0%ZGG G  E ))h "J$2$0% Z LS K0%))Z G "% 2M 0%E Z ii ))  "%205Z
DX%l  V%WX A5L>B>KMY5@%0%XZ  $$ E IL>t5tK050%ZZ G    E Itt10%Z Itt& 05V%Z WXMA%B>Y5@%X dS $ 5K"%Zo
DX%l  u @FlWM?D L>K0%Z G   5ZL%K ` dM0%KL[

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

W D L>K05Z G   %ZL%K ` dS0%KL5[

and the simple statement




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:

WCqSYJX@JWBDB>r5YJD%XqD E ` YJI>XBnDn X B0PYJ/X [%Z $ E] [%Z $ EM


YJXBDr5D%q  YJXBDXB  @FWB  l Y 1u [D%X Z $Y5@%E X  [%Z $ E   [ %Z $ E && <L<" I>tKJI>tI>K K G G  I I0a0aE E

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:

WCqqSD@JW?B>YJX@%D fdSB>LY5@%X["<? / l^Y n D X Y5 @5IX tt0%Z   "520%Z  05HJI  L5KL0aE] L 0%HJI1  L%KL0aE]_[" G 0  EJKM%ZK
 Itt05Z   "%20%Z
 u
 
allows us to call dSL[" by
A%?? dSL1["/  n  _Itt0%Z1
and in daL[" we have someting like:
YJV  f5qD DXB   "%2M0%Z B MDX & K0aEJKTn"5ZitSZ0aE501[0T"%n  [%KJI  %Z=JIdS0>K


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

A%?? dSL1[" / ^n   05HJI  L5KL0aE  \ EJK%ZK  >

Optional and keyword arguments are handled by explicit interfaces, that is with internal or module procedures or with 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, specifications and
statement of the procedure
concerned, either placed in a module or inserted directly:

DX%l

& Z0%KgI>qDZJjq?TE DV%K`W? XM0iA%dSB>YJXL<Y5B@%DLFX d5XdSIaB dLGLFdF Ia Id 0T#\"% nT K  ` nJ0RInJI[>1 [%KL" nJI[j  L K ` 0TL>K05Z G   Q\  
YJXBqD%qMD V?TA%V%D WXA%BSQLY5\@%X  \ 
nJB I YJ[jX  

q

D


?
J
Y

X

B

D

X

DqX%DlD? X5YJlmXBD5V5qWVXA%B>A%DYF@%X nJI1[
  n
n  nJI[]  & L G "[%KL" "%nTK ` 0 IjEF0%ZTnJI[%KML" 
DX%l V%WXA5B>Y5@%X daLLFd5Id
f@aYgXBD%q and B%qSr%DB arguments (see later article), a
An explicit interface is obligatory for: optional and keyword arguments,
f@YJXBD%q function result (later) and new-style array arguments and array functions
(later). It allows full checks at compile time
between actual and dummy arguments.

Fortran 90 Tutorial

11

Overloading and generic interfaces


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

YJXBV%D%WqMXVA%BSA%DY5@%=MX FddS
&& =010%ZL[ Fda0
EJ=MDF?dDMdSA%jBD% l  qD? JY X5l  9 OE#EJt=0Jd[dSL%n\L [ JdS0On"%Z

q

D


?

DV%WX%lXA%BSY5@%X
$ =MDF?dDMdSA%jBD% l  qD? YJX5l & $#E t0[L%nL[ JdS0On"%Z

q

D


?
 3F4> = JddS\
DX%l 
DX%l YJXBD5qVA%D


 "%2 >t Z0[LaE5L"<


` L%= ` t>ZM0[LaE5L"

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

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

G "  Iada0  LSK0%=Z5K0j no  o  " I $ EM

we might have

qDAFW&hq Y YJpDOV5WXA%B>YF@%X L>KM0%=Z%KM0j n   "I $ E


qYJXDB>D%? qMKMVL0%>=KMZA%0%D%=KMZ0i%n\KM0   nZM"Fd  "I $ E3hKM"  "I $ E1#4>
V%WMXqA%DB>Y5?@5X n\ 
nB>YF @%X n

D
5
X
m
l
5
V

W

X
%
A
qDX%Dl? YJ XlBD5Y qu VDX A%DYF@%X #4>1 YJXBDXB  YJX    "<I $ E
DX%l  V%WXA5B>Y5@%X L<>K0%=ZM%K0


and to integrate f(x, y) over a rectangle

V%WXWA%BSDY5@%X no  o


& ad " $ I  0 nJI[T["SKLjE nJI[%KL"<mn
qD? nJIno [  o
onoG  T L>KMo 0%=Z%KM0j n  g "I $ E 
DX%l


Direct recursion is when a procedure calls itself, as in

qDAFWYJXq BDYJpr5D%DOq V5WXA%B>YF@%X n[5K"%ZL  /\ qD WM?B  Z0E


YJV /  D  Z30a E]B  DX
D? ZD 0E  3
Z0YJVE  5n[%K"5ZL   a3

D
%
X
l
DX%l
qD WM?B 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

lS@ L  3] 
##L>L  3 i \  #L>  T#L j 3#L\ \U L> j#L  3    L>  #L>

DX%lTlS@

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 final iteration where i = n.


We note that a zero-sized array is regarded as being defined; however, an array of shape, say, (0,2) is not conformable with one
of shape (0,3), whereas

3  >  6

is a valid do nothing statement.


Assumed-shape arrays

qD?  lY u DX FY @%X #  35 \  4 >   


A%??  E#I  #>

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


the corresponding dummy argument specification defines 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


WCqSqD@FW?B>YgXlD Y E#IDX  $ YF>@%X


 u
       $ 


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

WCqqSD@JW?B>YJXlD Y EJD2Xt YFQ\@%X   


qD?  lY uu DX YF@%X   YF %D #>5  2\ %"  Z% & %ZZ%o [%ZM0%K0 $ "  EJK[F
2 "%Z%    
DX%l NW C2qa"%@FZ%WB>YJXD EJ2t


Fortran 90 Tutorial

13

ALLOCATABLE and ALLOCATE


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

u @FlWYJX?BD D2r5D%"%qZ%a %ZZ5o


Df%qSX%l@qrFDuqM@F l? WM ?lD Y u DX YF@%X        ??@A%B%C?D  2"%Z%
WqDD%lui2dS"%Z%aL< %ZZ5o
??@A5#B\D U >2M"%kZ%  / ^4< U6\ BB  EJK%KJI]E
l D??M@A%BD  2"%Z% 


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,

lD??M@A%BD #\  

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


Elemental operations and assignments

qD?   lY u DX 5Y @%X  35& >    


  EgHZK\#S & L5E >[K Z L%jZ E5 LZM[ "nJ I$ [[%KEJLK "<+  0 ZM 0a0FdSE#I0> KMKc a ETa%EZEFZL%=J%aoTdS"0<>sK 0[%K

We have already met whole array assignments and operations:




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):

f%qa@qrFqD?uTKl0YEJK DX Y5@%X


  
  
Zf%qYJXn\B  #  u   #6>    Q 3 U4  6 >   QR4 U4 U4 S Z
A@5XBV%W>XYJXA5B>Y5\@%X  Z
qqDD??  llYY u nDDXX #[\Y5Y5 @%@%$XX   Y5 %D  [\ $
nDX%l V%[ W XM$ A%uB>Y5@%X & #"%Z #E5[>"Fda 0bda "%Zn 0 IjE50%ngI  nJI[%KML" "%n [  $T$ 
DX5lmf5qS@rFq uRKM0aEJK n





WHERE

D%qD # T   >   3   &  G "L $O$ L G LaE5L"  o


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

DX%l D%qMD
D%qD #T    >
D? D  D%q3 WSD r%D
DX%l   D%qMD #>
Often, we need to mask an assignment. This we can do using the WHERE, either as a statement:


Fortran 90 Tutorial

14

Array elements

qMD?  l Y u DX 5Y @5X  35   53  >  



j 3]h3 . For a derived data type like
BefqD DKZ? L<t  0%K
I$ I

q

D


?

l
Y

D
X
F
Y
%
@
X




u
#

>
6

DX%l BefD KZMLt  0%K


Simple case: given
we can reference a single element of as, for instance,

we can declare an array of that type:

BefD  KZL<t  0%K1 l Y u DX 5Y @%X < 35 \ 4 S   MK %Z




and a reference like

K%Z /  4>

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

K%Z /  4> ) $ I

is an array of type real, and

K%Z /  4> ) $ I #4S

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

Array subobjects (sections)


The general form of subscript for an array section is



as in


  

qMD? j35  3F >


]]#L\3 h d 3 U \s>
]]##L\L\h3      6>
]]#mL\#hm35 3] 3 :  aU36\ U4m>h3
]] 3] Uh43  :33  4>

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


&& t2 `%Z" K c0 "%ZMn `"%"2 10R["  Iad5
&& Z0 G"%20%ZoRL K Z0L%GZ 0%$ Z>05E 0T0FdS"%Z0S$ Km0%Z "5nRZM"%2
&& G303b[%LaKM"%E Zm 0%E#I=  E5 [%ZLE tSK "%KTZ05n0%Z01[0 $
Za K2M" EF0[%KL "<

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

 m#m3] : U6\ : >T  # 3]U4\h6  7 >

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:

K%Z ) $ I

& L   %0 = 

@FWB or YJX@FWMB

dummy argu-

Fortran 90 Tutorial

15

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

&& 5E [  %Z  Z<a 0%ZM">


%Z Z%o E50[%KML"  ZMa "0>

jj3]3 h 3]3  3

depending on the circumstances or requirements.


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

KK%%ZZ\) <I 3] 3 ) I

&& % ZZ%o E50[%KML" EgKZJI[%KgI>Z0O["Jd5t"0<>K


[ "Jd5t"0<>KT"%n  %ZZ%o 0  0FdS0>K

Arrays intrinsic functions

lS@%BB Wf%qS? @FlWaA%B


u u

l "5KitSZ" $ I[5KT"%n 4OZ<aS"10O%ZZ5o>E


u5KZL dFI  KLt  L[%KML"

?X?e
A@FWpXB ?
uu YJXp?
f%qSW @FlWSA%B
u

BB ZgI0TL%n  OG   I10aEb%Z0OKZJI_0 D


X IZgId 0T0%L%ZTn "5nm>Ko ZJI1G 0R I10 0R 0FdSLEi0SKK>ZJE I0L %ZZMJd5%to  0  JY V  Xe U -   B MDX
uuL<LFLFd5d5IaIadd GG   II0T0TLL  %%ZZZZ55oo
f ZM" $ I[%Kc"%n %ZZ%o 0  0FdS0>KSE
Id "5nm5ZZ%o 0  0JdS0>K>E

?%CS??@FWM@X%A%lBD%l
Y5%fD D
WCS@FWMX%l

? ZZ%oT  "[%KML"PEJKM%KJIjE


`B "52t0%Z0O$"%nLJdS0j E5%LZ"Z5o "IQ"%$Z EbE5"%[n  5NZ 5ZZ%o
W t"5tK0%Z $ ILJddS0j0%ZE5L"%"n 0 "I0JdS$0>EbK>"%Ebn LN5Z%ZZ%Zo %o

ufD%qSA r5D
Wf%XqfDM%A l

uf 05Z[F=0hI  $%Z0%ZOZ%dSo aLES K"T %ZZ%o "%nTZ<a


qW 0<t  L[5K0O%ZZM%o  o  $$ LS=T $ LJdS0jE5L"
t[F <N%ZZM%oT"%n Za "0TLSK"T %ZZ%o I $ %0 ZOdSaE

Vector and matrix multiply

Array reduction



Array inquiry

 


Array construction

qD MfD

q 0E ` t0b %ZZ%o

DB%A q@ 1XYgVYJBVfB@ D

DBA L5Z$ [I"%n n5ZmE ` E `L%nL%nK K


ZMjE#t"E50O"%n  %ZZM%o "%nmZ<a K2"

uu YJX?M?M@@AA

?? "[%KL"< %" nTnL%Z>EgKcda LFd5Id


"[%KL"< %" nTnL%Z>EgKcdaLLFd5Id

Array reshape
 

Array manipulation
 

 

Array location


GG   I10RL<P<N%ZZM%o
 I10RL<P<N%ZZM%o

Fortran 90 Tutorial

16

6. Pointers
Basics

f@aYgXBD%q attribute; they are not a distinct data type (and so no pointer arithmetic is possible):
qD?  f@aYJXBD%q _G %Z

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):

??M@A%BD  G % Z

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

G %Z RG %Z  4  6


the value of the target of G %Z is used and modified. Pointers cannot be transferred via I/Othe statement
%q1YJBD \ G %Z
writes the value of the target of G %Z and not the pointer descriptor itself.
A pointer can point to other pointers, and hence to their targets, or to a static object that has the TARGET attribute:
qqDD??  fB%@aqSYJXr%DBBD%q  "  s0[%K
GG %Z   -- "  s0[%K   K%Z=0%K "  s & t1"L>K05ZTaEE5L5=JadS0SK
%Z K5Z=0%K " s
but they are strongly typed:

YJXBDr%D%q f@aYJXBD%q
G %Z  - L SK G %Z   L >K G %Z

& L   %0 =  RKogt0aE d5IjEJKOdS5K[ `

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

BefqD D0>? KZo


& K oJt0On"%Z E#t5Z>E50idS%KZML
G

YBJXeBfDD r%D%q IL 0 $ 0 f@aYgXBD%q
   0 K & 1%" K0OZ0[ISZ>E5L"
DX%l BefD #0S0SKKZZoo 

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

BefD Q0>KZo1 f@aYgXBD%q   [ ` L

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

[[ `` LL )) GL $ I10 0


[ ` L ) 0 K

[[ `` LL )) 00
[ ` L ) 0

KK )) GL $ I10 0
K ) 0 K

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

Fortran 90 Tutorial

17

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

lMX5WD??>??YJV@eA%BD /t  H  && n"%ZTZ0%KJISZJL>= EJK! "%Z5=0 !


/t  H
n "%Z E50%KKML>=TK" I 
lD??M@A%BD on its target without X%W??>YgVe ing any other
Some care has to be taken not to leave a pointer dangling by use of
pointer referring to it.
 @AaYJBD%l can test the association status of a defined pointer:
The intrinsic function
YgV   @AYJBD%l /t"LSK0FZ1 B DX




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

YgV   @AYJBD%l /t"LSK0FZ  K5Z=0%K B DX






Pointers in expressions and assignments

o  C AR
qqDD??  Bf%@aqSYJXr5DBBD%q    35  35 > [j35 \#35 >^Z 35 >hE<35 \ < 35 >
YJXBDMr% D%q d5I  K  j        o\  
lS @ d5YJIV  K  3]U4 B DX
o d5I - K Z  3
& " $ %KidS" G 0FdS0SK
  --O [
D? D & " $ %KidS" G 0FdS0SK
o  -  E
DX%l YJV B- ZW?
& ["Fdda" [  [I  %KML"
DX%lTo lS@ u u #  

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):

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

BefD #0>KZo f@aYJXBD%q   n L%Z>EJK  [I>ZZM0>K


 nL%Z>EgK [<I>ZZ0SK

the assignment causes first to point at current, whereas

nL%Z>EgK  [<I>ZZ0SK

causes current to overwrite first and is equivalent to

nnL%L%Z>Z>EgEgKK )) GL $ I0 O0  [[ I>I>ZZZMZM00>>KK )) GL< $ I0 0


nL%Z>EgK ) 0 K  - [ I>ZZM0>K ) 10 K

Fortran 90 Tutorial

18

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

qD?  f@YJXBD%q  j    


??@ A5BD #] ; \ ; >
A%??  E#I  #>
WCqSqD@FW?B> YgXD E#I  #[>
[j    

Pointer functions
Function results may also have the
function, as in

f@aYJXBD%q

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

WqDD? $ %K `  $M 0%Z


qD?  f@3F YJX >B D%q  o\  
 o ["FdFt[%K\ 
`
where the module $ %KM  $ 0%Z contains
V%WXqA%DBSY5?@%X f[@"Jd5YJtXBD%[%qK  
     ["Jd5t[%K   

q

D


?
&  t>Z"YJX[B0 D$ I>r5D%Zq0 K"OZ0FdS" G 0 $ It  L[%K0aEknZ"Fd K ` 0R5ZZ%o
& V L< $ K ` 0 Iad  0%ZT"%n $ LaEJKL<[%K G   I0aE] 
??@A5BD 
#["Fd5t[5K\&U/\A "<5t> oTK ` 0 $ LaEJKML[%K G   I0aEbLSK"T["Fd5t[%K

DX%l V%WXA5B>Y5@%X ["Fd5t[5K


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

Arrays of pointers
These do not exist as such: given

BefD Q0>KZo1   Z "%2>E1/\

then

Z"%2>E ) 0 K

& L   %0 =M 

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

Fortran 90 Tutorial

19

BefqD DZ?"%2 f@YJXBD%q


  \Z   
DX%l BefD 

and then defining arrays of this data type:

BefD  Z"%2   1E /\ K\/ 

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

lS@ L ?? @A53]^BD


 K #L> ) Z 3  L> &    " [%K0iZ"52mL "%n  0>=K ` L
DX%lTlS@

The array assignment

E  K

is then equivalent to the pointer assignments

E#L> ) Z  - K #L> ) Z

for all components.

Pointers as dynamic aliases


Given an array

qD?  B5qSr%DB   K  1 0j35  \ 35  S

that is frequently referenced with the fixed subscripts

K  0] d    t  H 

these references may be replaced by

qD?  lY u DX YF@%X      f@YJXBD%q  2L< $ "%2


 $  -   
2L "52 KM 0j d   t H
The subscripts of window are 3  Jd  3 , 3  Ht  3 . Similarly, for
K5Z ) I


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

K5ZJI  - K %Z ) I

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

K5ZJI 3] 4>

DJWYJp?DXA%D

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 file name is
.

sFMZ  [[  Z   [  Ia

<tI  %uM $ q

tt =  n

Fortran 90 Tutorial

20

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

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

Y u f?>Y5AaYJBTX@5XD

at the beginning of each prorgam unit.

PARAMETER attribute

f %q u DBD%q attribute and the constant values to a type statement:


qBDef?D  lY u DX Y5@%X fQ56>q  fD %BqD%q u DBD%q   nL0 %$T QR    3   4   S  
  K  KZMLt  0%K U O# U U > 
 KZL<t  0%K1 u

A named constant can be specified directly by adding the




DATA statement

l B
llBefBBD  KSKZ35%L<tKZ L<0%t K  0%9%K\7  K> 3] 9%O7 K#m4  h3  U4  >mg\ K4 ) I   
lB 5L ZhZ%s\o\^<S3   C ! a35 35 a35  a3 !  @ ! ::\!  \! nn ! 

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:

&& "  To "<0m["Jd5t"0<>KT"%nTK4TLL5KL  L 0 $


"  To  E50[%KML" "%n %ZZ5omL<L%KL  L 0 $

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

AA 55qqA%A%BBD5D5qq  FdSJY X50]l  7  8 J4 ? DX


  S sL\  4 > l Y u DX 5Y @5X  7  8     FdS0



Initialization expressions

lMB f %q u DBD%q


Mq DfDB D?DA%BD5l YJXB JY X%l B5qY u D?DA%BD%l qD? Y<X%l
qD fD B%qMX VD5q
YJXBDr%D%q  f5q u DBD%q   "S=  D?DA%BD5l qD? YJX5l  3g4>_%ZZ%o #6>  # ]3 U4\U6m>

and
statements, or in specification 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
:


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


WCW qSD@FWB>YgXD E   d h[>


qMqMDD?? dS " ll$ YY u DDXX Y5Y5@5@5XX  W CS @JW X%l

8


  3 
YAgXB%qDr% D5A5qBD%qu ?DX 
AqMD%q? A5BDD%q?D A%?BDD%X l >dq D? ?DX YJXJQ[>l  f%qDMAaY Y5@%X

#4
 >








d[
[[

&& [ "SKLjE `
& a I>EKMEQIa"FdSdS%0 $KL[OE %tZZ0 5%o ZZM%o
&& aEEQIadS0 $   0>=K `
& t>I>ZKM0"F[dSLa%E5KLL"<[O""%n s 0[%KK 2ML[0OK ` %K "%n 

Fortran 90 Tutorial

21

PUBLIC and PRIVATE


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

qYJDXB?Dr% D%f%q WC?>f%q1YFAYJpBD    o 


 I  G  2


& $ 05nI  K

and the statement form is

f%f%WqCMYg?>pY5BA D   U o   @%fD%qY5r5BX @FqDX B   $$   @%fD%qMB@Fq


 I  G  2 
u   
Q>


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

f%f%qWCMYgp?>Y5BA D

& E505K>E $ 0%nI  KTn"%ZOdS" $ I  0


`
  "  o %K LE
f5WC?>Y5A f%, qtheYgptype
f5WC?>Y5A and its
For a derived data type there are three possibilities: the type and its components are all
is
%
f

q
J
Y

p



B
D

B
D
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):
u @FlWf%q?DYJpdSBLD 0
BefqD D f%? WC?>Y5A U LaEgK
UEJoK1 f@aYJXBD%q  0 K

B

e

f
D


a
L
DBX%elfD BefD  LEJK
  LaEJK  KZ00

DX%l u @FlWM?D dSL10
USE statement

W D

To gain access to entities in a module, we use the 


the same as a local one:

W D dSL0   "[   LaEgK  -  aL EJK




or to restrict the used entities to a specified set:

W D dSL0  @%X?e   aL EJK




These may be combined:

W D dSL0  @%X?e   " [   LaEJK  -  aL EJK




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

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
classification and with those that have so far been omitted.
All intrinsic procedures can be referenced using keyword arguments:

A%??clMBD X%l B>Y u D  >B Y u D  K 

and many have optional arguments. They are grouped into four categories:
1.
2.
3.
4.

5C #>

elemental work on scalars or arrays, e.g. 


;
inquiry independent of value of argument (which maybe undefined), e.g.
transformational array argument with array result of different shape, e.g.
.
subroutines, e.g.  

e B D Mu A%?@A

q f%DqMDMAaY fD YF@%#X #>  ; ;


 

The procedures not already introduced are:


Bit inquiry

C1YJB YF%D

X Iad  0%Z "%n  L%K>EbL K ` i0 dS" $ 0 

Bit manipulation

CMYgBDX%l B
Y<Y<CSCA%YJ?%B q
Y<YgCD@FqDB
YFY @Fq VB
XMY @%B VBA

C? L%KTK0aEgKL>X%l=
AC "% =0L5Z[   L%K
L%0%KK 0 L%KK Z[%KL"
DY [  IjE5L G 0 @F@Fqq
?A "%=[ L Ij[E5L G 0E ` ` L%nK
? L%"%Z=[<LI[ % Zm["FE d5tL%n K0FdS0SK

 
 

Transfer function, as in

YgXBDr%D5q   L  B%qX V D%q  !   [ $ ! ^ > & Z0t  [0aEUt5ZK "%n DFWYJp?DXA5D




Subroutines

lMp%CBDYJB X%l B>Y u D


qMqMu X%X%lSlS@@ uM X%WDuD%CMl D%q
e BD uMuM A%?@A


@A  KL $ %K0T $ "%ZOKLJdS0


q "0%tKJISLZJ0ajE Ek tjL%K>E50E I $ "%Z $ "Fd Iad  05Z>E
 [[[[00EEEE KK"m"mE5EJo>00EJ$K0Jd [  "[F

Fortran 90 Tutorial

23

9. Input/Output
Non-advancing input/output
Normally, records of external, formatted files are positioned at their ends after a read or write operation. This can now be overridden with the additional specifiers:

%Dl@FqpXA%D  ! X@\!
Y55D   0"%E5ZL 0   0 


#$"tS05KnLI" Kc  La E qD! e5lD


#"tSKL"   qD5l

!
"<"<  oo

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:

AYJXB%qDMr5D%A%qBD%q Q6> a0%o


qD%l /I1IL%K L5 K !h E56>L  0 !  %lMpXA%D  ! X@\!  FY %D  5E L 0\ D@Jq  99 US0%o
& S90%9 o  LaEk"%K L "0OZ!0! ["5Z $
S 0%o\EFL 0  3   


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

q%qD%YJlBD ##  !!  Y 35 >!  ! % lpXMA%D  ! XM@\t>! Z LFdS! D0> KIa05Zid  10%0Z K t>ZLFda0 Iad  0%Z  !

New edit descriptors

The first three new edit descriptors are modelled on the

Y edit descriptor:

binary,
octal,
hexadecimal.


There are two new descriptors for real numbers:
DX engineering, multiple-of-three exponent:  4a3 :  - 4a3  : D  6  D XM  >4 
D scientific, leading nonzero digit:  4a3 :  - 4  3 :5D  4  D  4>
r
D V Y ? 
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:

BefDYJXBEJDKZMr5D%L>q = `
 0>?=DKX 
A

%


q
M

%
A

B
%
D
q
DBX%elfD BefD EJKZL >= 4 > 2M"%Z $
qD%l <Q\EJK ZL! S =Y 4\     K! 0 KK 0 K


Fortran 90 Tutorial

24

New specifiers

%@ fDX and YJXFW1YqD statements there are new specifiers:


f@A%B>Y5YJBS@5XY5@%X  !! q D%Y l !!
!/! q%qD>YYJBJX5D l ! ! !!/qfDf%lDX55qlYJ! BD !
lfD%l?>Y u  !! efD @ ! B%qS@%f MD !i!! XFWS@\@%! BD ! ! X@%XD !

YJX FWY<qD there are also
and on the
q%qD%YJlBD   ! eD !
! X@\!
!/WX X@ X !
qD%l 5qYJBD  
On the




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

YJXFWY<qD  5Y @5?DXr%B m 0<>=K `  L%K0Fd3  L5K0FdS4\ 


qMDA%? , or to check that a list is not too long. It is in the same processor-dependent units as qMDA%?
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

hexadecimal, 2, 20
implicit typing, 20
initialization
of expressions, 20
input/output
new edit descriptors, 23
new specifiers, 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

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

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

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

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

DATA, 20
defined 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
specification of , 20

generic interfaces, 11
generic names, 11

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

heap storage, 13

scope, 21

formatted files, 23

25

Fortran 90 Tutorial
section, 14
shape, 7
significant 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

Das könnte Ihnen auch gefallen