You are on page 1of 28

Fortran 90 Tutorial

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

1 Language Elements 1
2 Expressions and Assignments 6
3 Control Statements 8
4 Program Units and Procedures 9
5 Array handling 12
6 Pointers 16
7 Specification Statements 20
8 Intrinsic Procedures 22
9 Input/Output 23
Index 25

Full details of all the items in this tutorial can be found in Fortran 90/95 Explained, by
M. Metcalf and J. Reid, (Oxford, 1996), the book upon which it has been based.

Fortran 90 contains the whole of FORTRAN 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 as-
sistance in preparing this tutorial.

Version of October 1995


Fortran 90 Tutorial 1

1. Language Elements

  

The basic components of the Fortran language are its character set. The members are:


the letters and (which are equivalent outside a character context);


the numerals ;
the underscore and
the special characters

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


/ 10%2
From these components, we build the tokens that have a syntactic meaning to the compiler. There are six classes of token:
53 46 /789:; 
35?4?6 @A5B%C?D "<>=
Label:
Constant:
Keyword:
  $$ 
EF" %G 0 0%HJI%KML" T(can - have up to 31 characters, including a ).
Operator:
Name:
Separator: NOPQRSP   +U)
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
A@%XpD%qB (CERN Program Library Q904). Its
options are:

significant blank handling;

A@%XB>YgX%WD
indentation;
DX%lTlS@
replaced by ;
DX%l
YJXBDr5D%q 4
name added to subprogram statement; and
etc. syntax converted.
A@%XpD%qB sFZ  [[  Z   [  I (130.246.8.23). The direc-
tory is tI  %u $ q
The source code of the program can be obtained by anonymous ftp to
and the file name is . [" G 0%ZK  n
Fortran 90 Tutorial 2

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

YJXBDr%D%q
types they are:

3    64 :9:  35
Examples are:

, a named constant, say K2"  oK0aE :


 
YgXBDr%D5q  f%qM u DBD5q  K2"  oK0aE  D?DA%BD%l YJXB YJX%l  7 
for the default kind; but we may also define, for instance for a desired range of
 
to

 35354 %K6 27 " 5K 2o"K 0oE K0aE


that allows us to define constants of the form

Here, K2"  oK0aE is the kind type parameter; it can also be a default integer literal constant, like
3546 7 4
YJX%l function supplies the value of a kind type parameter:
but use of an explicit literal constant would be non-portable.
The
YJYJX%X%ll 3M


q3F %KX2r5D"  oK0aEM
qMXr%D <35 %K2"function
and the
 oK0Esupplies

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

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


C@ !! a35 a3F7 a835 a9:\3 !!
Also, in

 ! a35 %35n46 !
qD?
There are at least two real kinds the default, and one with greater precision (this replaces
lS@FWC?DTf%qDAY Y5@%X ). We might

specify
YgXBDr%D5q  f%qM u DBD5q U ">=  D?DA5BD%l qMD? YJX%l    


     
3 :  "<>=
for at least 9 decimal digits of precision and a range of to , allowing

fqM5qYJX%DXlAar%DY3 Y5 :@% X :  "<3 > =:   "<>=>


Also, we have the intrinsic functions

<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 

<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

lYFraYJB  
of their arguments). These functions are important for writing portable numerical software.

Df YJ?@%X  

War%D    
Number of significant digits
 
Almost negligible compared to one (real)

u YgXDD ff@5@5XXDDXXBB  
 
Largest number
  

uf%qMDAaY YF@%X  
Maximum model exponent (real)
 

q5lY    
Minimum model exponent (real)
 
Decimal precision (real, complex)
qXr%D  
 
Base of the model
B>YgXe  

Decimal exponent range

Smallest postive number (real)
Fortran 90 Tutorial 3

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


'<!! N"%EJ' KK `ZML0%>Z =' '! ! !!


HJI1"%K0
4 ! !
(the last being a null string). Other kinds are allowed, especially for support of non-European languages:

YgX%l function:

YJX%l  !  AaYY ! 
and again the kind value is given by the


?@raY5A%?
 V? D  
Here, there may also be different kinds (to allow for packing into bits):

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    0

YgX%l and ?DX specifiers
where the optional
are optional and the following works just as well:
specifier replaces the form. The explicit

 A %qMA%BD%q Q4\ S1sL> S<sL %2M"%Z $


For derived-data types we must first define the form of the type:
BefDA Mt%q0%ZSE5A%"BD%q

q 
D


 ? 35 S^%=MJdS0 0
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
) qualifier:

o"I ) 5=0
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.


Fortran 90 Tutorial 4

Definitions may refer to a previously defined type:


BefqD Dt"? L<>K
DBX%elfD BefD t "oL>K

B 
e KfZD L/t>"=L> K0 k\   [
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 ZM0  :
K)) K))o K)) 05K[ 
)
We note that the qualifier was chosen rather than because of ambiguity difficulties.

Arrays are considered to be variables in their own right. Given
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\ && Z <a "0


jj#dS6  t4S#L\     d]  &&_ZG <0 a[%K"%KZ 2"E#I  ` E5[%ZLtSK
05Z"  0 >=K
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 compo-
nents:
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 5

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? %YJYJlX%lAWD 5qB%q  n"5Z  CAaAYY E5/0%B%K1qW D 
  

q?B%qDDXfY D B5qB Y u ApD%%qXYJVe E n3]"% Z EF"4\ 0 "%n  E505K 


 


 n"%Zb  "%n  E505K



u
Fortran 90 Tutorial 6

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 =
A  %
 
q M
 %
A 
B %
D q ;  0> =K `
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  JE KZS35 aEJKZ4  && Fd IjEJK $ 0%nL<0O"t05Z%KL"<


[EJ`KZM5Z6 6  [EJ``KZS5Z3 4["1[ `[%%ZK 6 EgKZ4 && Fd<L Ij>KEJKZL]$ 0E5$LL<[O"0Ot1"0%tZ05%ZK%"5KZTL""<  o
[ 5Z>3 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
 ["[%K 
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
GG 0[5K"%Z6  Sd %KZML    GG 0[5K"%Z>3 RRGG 0 [5K"%Z4
0[5K"%Z6  ad %KZL KLFdS0E 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 7

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


JY XBD%q@FlVW?A5DTDOf%@%qSfD%@qMA5D%lBW@Fqq D Q>
DX%l u YJXBD%qVMA%D EJKZLS= ["1[%K
and an example of part of a module containing the definitions of character-to-string and string to character assignment. The

u @JlWBe?fD D EgKEJKZZL>LS=M= %KoJt0 WCBqSef@JWD B>YJXD [ %K" E5 YJXaBEDEFXL%B=J @FWE B  [>
string concatenation function was shown already in part 1.

A ) 5qA%<BEJKD5Zq L S?=DX  >S YJXBD XB  YJ X  




A YJXB%qMDMr%D%A%qBD%q 0<>?=DKX ` ; [E
DYJX5XlmBD%qBeVfMDA%DOEJKZL< >Y5=r%X  DX aB   EgKZL>=M $ %K EE ) EgK0>Z=L>K `=M $ %?KD X  #[>[ 



u   DWX%lCqS@JWWCB>qaYJ@FXWD B>YJXD [ %K" aE5 aEE5L%=J


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 


BA e5fqD A%<BEJKD5Zq E5LS %?K=D"X  [ YJXaBEDYJEFXXL%BB=JD  XYJ#BX[  ^@FWEB 


F
@ 
l M
W 
? O
D %
f S
q 
@ %
A %
D 
l 
W M
q D #EJ>K ZL<>= ["<[%K D[ X%l E W) CEJqSKZ@FWL SBS=YJX $D %>KMSj 3  E )  0<> =%K `  

[E
u
A@5XDBX5>lPYJX YgXBD%qVA%D 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
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


[ cG    && 22 `` ""  0b0b%%ZZZZ%%oo $aLEGE5LaL%=JE5aLda"<0>KT $"%n aE5E[E5L%=ga%ZdS0G>K  I0
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
VV%q?M@@FA%qB>Y5@%X u @FlW?@ #  EF"OL>K05=0%Z
qqDB fD AafYJXM@5Xr DXB fMAaYJXr


 A%?D






Like all FORTRAN 77 functions (


YgX , %C , etc., but not ?DX ), these are array valued for array arguments (i.e. are elemental).
 
Fortran 90 Tutorial 8

3. Control 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  Iad  05Z && %X W u CD%q "%n KoJt0TLSK0%=0%Z
 O G   I0aE  0  "%2
 

A% D aE5#L5 S=J  



3 & "  om
A% D aE5L53 =J   

&  OG   I0aE   " G 0T
DX%l  aDE5?L5=JDA%B 



3
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

D %
X T
l S
l @  L05 Z "I>K0%Z


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 9

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

W CqqSD@JW?B>YJXD "I>K0%Z
statement function:


 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[


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

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    $$
DX%l u YJXBD5qVA%D  L>KM0%Z G   E
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[
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
DX%l
concerned, either placed in a module or inserted directly:

& 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  \ 

q 
D 
 ? J
Y 
X 
B 
D 
X nJB I YJ[jX  

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
f@YJXBD%q function result (later) and new-style array arguments and array functions
An explicit interface is obligatory for: optional and keyword arguments,
(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

q 
D 
 ? EJ=MDF?dDMdSA%jBD% l  qD? JY X5l  9 OE#EJt=0Jd[dSL%n\L [ JdS0On"%Z
  "%2 >t Z0[LaE5L"<
DV%WX%lXA%BSY5@%X   


q 
D 
 ? $ =MDF?dDMdSA%jBD% l  qD? YJX5l & $#E t0[L%nL[ JdS0On"%Z
 ` L%= ` t>ZM0[LaE5L"
DX%l    3F4> = JddS\

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

Recursion
Indirect recursion is useful for multi-dimensional integration. To calculate
G "  Iada0  LSK0%=Z5K0j no  o  " I $ EM
we might 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\ 

D 5
X m
l 5
V 
W 
X %
A nB>YF @%X n
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
Here, we note the
qD WM?B clause and termination test.

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] 
A zero-sized array is handled by Fortran 90 as a legitimate object, without special coding by the programmer. Thus, in

 ##L>L  3 i \  #L>  T#L j 3#L\ \U L> j#L  3    L>  #L>
DX%lTlS@
no special code is required for the final iteration where i = n.
We note that a zero-sized array is regarded as being defined; however, an array of shape, say, (0,2) is not conformable with one

3  >  6
of shape (0,3), whereas

is a valid do nothing statement.

Assumed-shape arrays

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


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

A%??  E#I  #>




the corresponding dummy argument specification defines only the type and rank of the array, not its size. This information has

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


to be made available by an explicit interface, often using an interface block (see part 4). Thus we write just

       $ 

 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,

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


as in

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 EQUIV-

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


ALENCE). An example, for establishing a work array for a whole program, is

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

lD??M@A%BD #\  
explicit lower bound and allocate several entities in one statement. To free dead storage we write, for instance,

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

Elemental operations and assignments

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


We have already met whole array assignments and operations:

  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


In the second assignment, an intrinsic function returns an array-valued result for an array-valued argument. We can write array-

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


valued functions ourselves (they require an explicit interface):

     
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


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


D%qD #T    >


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

  3 


N

DX%l D%qMD 

D%qD #T    >


D? D  D%q3 WSD r%D


DX%l   D%qMD #>


 


Fortran 90 Tutorial 14

Array elements
 qMD?  l Y u DX 5Y @5X  35   53  >  
 j 3]h3 . For a derived data type like
Simple case: given
we can reference a single element of as, for instance,
BefqD DKZ? L<t  0%K

q 
D 
 ? 
l Y 
D X F
Y %
@ X 
  I$ I

DX%l BefD KZMLt  0%K u  #
 >
6 
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 > && t %ZK "%n "10cZM"%2
]]#L\3 h d 3 U \s>
]]##L\L\h3      6> && t2 `%Z" K c0 "%ZMn `"%"2 10R["  Iad5
]]#mL\#hm35 3] 3 :  aU36\ U4m>h3 && Z0 G"%20%ZoRL K Z0L%GZ 0%$ Z>05E 0T0FdS"%Z0S$ Km0%Z "5nRZM"%2
]] 3] Uh43  :33  4> && 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
@FWB or YJX@FWMB dummy argu-
ment.
Arrays of arrays are not allowed:

K%Z ) $ I & L   %0 = 


Fortran 90 Tutorial 15

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

jj3]3 h 3]3  3 && 5E [  %Z  Z<a 0%ZM">


%Z Z%o E50[%KML"  ZMa "0>
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 l "5KitSZ" $ I[5KT"%n 4OZ<aS"10O%ZZ5o>E


Vector and matrix multiply

u u u5KZL dFI  KLt  L[%KML"


?X?e BB ZgI0TL%n  OG   I10aEb%Z0OKZJI_0 D
Array reduction

A@FWpXB ? X IZgId 0T0%L%ZTn "5nm>Ko ZJI1G 0R I10 0R 0FdSLEi0SKK>ZJE I0L %ZZMJd5%to  0  JY V  Xe U -   B MDX


uu YJXp?

uuL<LFLFd5d5IaIadd GG   II0T0TLL  %%ZZZZ55oo
f%qSW @FlWSA%B f ZM" $ I[%Kc"%n %ZZ%o 0  0FdS0>KSE
 u  Id "5nm5ZZ%o 0  0JdS0>K>E
?%CS??@FWM@X%A%lBD%l ? ZZ%oT  "[%KML"PEJKM%KJIjE
Array inquiry

Y5%fD D `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
  

WCS@FWMX%l


ufD%qSA r5D
Array construction

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


Wf%XqfDM%A l

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


qD MfD q 0E ` t0b %ZZ%o


Array reshape
 

DB%A q@ 1XYgVYJBVfB@ D DBA L5Z$ [I"%n n5ZmE ` E `L%nL%nK K


Array manipulation
 
 
  ZMjE#t"E50O"%n  %ZZM%o "%nmZ<a K2"
uu YJX?M?M@@AA ?? "[%KL"< %" nTnL%Z>EgKcda LFd5Id
Array location
 GG   I10RL<P<N%ZZM%o
"[%KL"< %" nTnL%Z>EgKcdaLLFd5Id  I10RL<P<N%ZZM%o
Fortran 90 Tutorial 16

6. Pointers
Basics
Pointers are variables with the
f@aYgXBD%q attribute; they are not a distinct data type (and so no pointer arithmetic is possible):
qD?  f@aYJXBD%q _G %Z
They are conceptually a descriptor listing the attributes of the objects (targets) that the pointer may point to, and the address, 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
DX%l BefD #0S0SKKZZoo     0 K & 1%" K0OZ0[ISZ>E5L"
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 [[ `` LL )) 00 KK )) GL $ I10 0


[ ` L ) 0 K [ ` L ) 0 K ) 0 K
but we would normally define additional pointers to point at, for instance, the first and current entries in the list.
Fortran 90 Tutorial 17

Association
A pointers association status is one of

undefined (initial state);


associated (after allocation or a pointer assignment);

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


disassociated:

/t  H n "%Z E50%KKML>=TK" I 


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


YgV   @AYJBD%l /t"LSK0FZ1 B DX


 

or between a defined pointer and a defined target (which may, itself, be a pointer):
YgV   @AYJBD%l /t"LSK0FZ  K5Z=0%K B DX
 

Pointers in expressions and assignments

o  C AR
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):
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 -

o  -  E & " $ %KidS" G 0FdS0SK
DX%l YJV B- ZW?
DX%lTo lS@ u u #   & ["Fdda" [  [I  %KML"
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
f@aYJXBD%q attribute; this is useful if the result size depends on calculations performed in the
function, as in
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  

q 
D 
 ?      ["Jd5t[%K   
&  t>Z"YJX[B0 D$ I>r5D%Zq0 K"OZ0FdS" G 0 $ It  L[%K0aEknZ"Fd K ` 0R5ZZ%o
??@A5BD  & V L< $ K ` 0 Iad  0%ZT"%n $ LaEJKL<[%K G   I0aE] 
 #["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


DX%l BefD    \Z   
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
DX%lTlS@  K #L> ) Z 3  L> &    " [%K0iZ"52mL "%n  0>=K ` L
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>


The subscripts are as those of tar itself. (This replaces yet more of .)
DJWYJp?DXA%D
sFMZ  [[  Z   [  Ia <tI  %uM $ q tt =  n
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 .
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
A named constant can be specified directly by adding the
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  
 KZL<t  0%K1 u   K  KZMLt  0%K U O# U U > 


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

llBefBBD  KSKZ35%L<tKZ L<0%t K  0%9%K\7  K> 3] 9%O7 K#m4  h3  U4  >mg\ K4 ) I    && "  To "<0m["Jd5t"0<>KT"%nTK4TLL5KL  L 0 $
lB 5L ZhZ%s\o\^<S3   C ! a35 35 a35  a3 !  @ ! ::\!  \! nn !  "  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
The values used in
lMB f %q u DBD%q
and statements, or in specification statements with these attributes, are constant expres-

qD fD B%qMX VD5q


arguments and results, and the six transformational functions ,
Mq DfDB D?DA%BD5l YJXB JY X%l B5qY u D?DA%BD%l qD? Y<X%l
sions that may include references to: array and structure constructors, elemental intrinsic functions with integer or character
, , ,
  and  :
YJXBDr%D%q  f5q u DBD%q   "S=  D?DA%BD5l qD? YJX5l  3g4>_%ZZ%o #6>  # ]3 U4\U6m>


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[> && [ "SKLjE `
qMqMDD?? dS " ll$ YY u DDXX Y5Y5@5@5XX  W CS @JW X%l


  & a I>EKMEQIa"FdSdS%0 $KL[OE %tZZ0 5%o ZZM%o





YAgXB%qDr% D5A5qBD%qu ?DX 


 
  3   8
d[ && aEEQIadS0 $   0>=K `
AqMD%q? A5BDD%q?D A%?BDD%X l >dq D? ?DX YJXJQ[>l  f%qDMAaY Y5@%X & t>I>ZKM0"F[dSLa%E5KLL"<[O""%n s 0[%KK 2ML[0OK ` %K "%n 


[[

  #4
  >
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  & $ 05nI  K
  I  G  2
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
%
f 
q J
Y 
p 
 
B D
For a derived data type there are three possibilities: the type and its components are all
components (the type only is visible and one can change its details easily), or all of it is

B D is
(for internal use in
the module only):

u @FlWf%q?DYJpdSBLD 0
BefqD D f%? WC?>Y5A U LaEgK

B 
e 
f D   a
L UEJoK1 f@aYJXBD%q  0 K
DBX%elfD BefD  LEJK
   LaEJK  KZ00
DX%l u @FlWM?D dSL10
USE statement
To gain access to entities in a module, we use the 
W D statement. It has options to resolve name clashes if an imported name is
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

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:
5C #>
q f%DqMDMAaY fD YF@%#X #>  ; ;
1. elemental work on scalars or arrays, e.g.  ;
2. inquiry independent of value of argument (which maybe undefined), e.g. 
3.
4.
e B D Mu A%?@A
transformational array argument with array result of different shape, e.g.
subroutines, e.g.   .
 

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 C? L%KTK0aEgKL>X%l=
AC "% =0L5Z[   L%K


Y<Y<CSCA%YJ?%B q
Y<YgCD@FqDB
 L%0%KK 0 L%KK Z[%KL"
 
DY [  IjE5L G 0 @F@Fqq
YFY @Fq VB ?A "%=[ L Ij[E5L G 0E ` ` L%nK
XMY @%B VBA ? 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 @A  KL $ %K0T $ "%ZOKLJdS0
qMqMu X%X%lSlS@@ uM X%WDuD%CMl D%q

q "0%tKJISLZJ0ajE Ek tjL%K>E50E I $ "%Z $ "Fd Iad  05Z>E
 e BD uMuM A%?@A



 [[[[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 over-
ridden with the additional specifiers:
%Dl@FqpXA%D  ! X@\! #$"tS05KnLI" Kc  La E qD! e5lD !
Y55D   0"%E5ZL 0   0  "<"<  oo


 #"tSKL"   qD5l
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
Y edit descriptor:
C
The first three new edit descriptors are modelled on the

@ binary,
 octal,
hexadecimal.

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


There are two new descriptors for real numbers:

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 = `
A  %
 
q M
 %
A 
B %
D q  0>?=DKX 
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
On the
%@ 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 !
  

  


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

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


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


and this is useful to set


qMDA%? , or to check that a list is not too long. It is in the same processor-dependent units as qMDA%? and thus
is a portability aid.
Index
actual argument, 10, 12, 18 hexadecimal, 2, 20
aliases, 19
ALLOCATE, 13 implicit typing, 20
argument, 10 initialization
array construction, 15 of expressions, 20
array constructors, 4 input/output
array elements, 14 new edit descriptors, 23
array inquiry, 15 new specifiers, 24
array location, 15 non-advancing , 23
array manipulation, 15 inquiry functions, 2
array reduction, 15 INTEGER, 2
array reshape, 15 INTENT, 1, 7, 9
array sections, 4 interface, 6
array subobjects, 14 interface block, 10, 11, 18
arrays, 4 intrinsic functions, 7, 15, 17, 20, 22
arrays intrinsic functions, 15
arrays of pointers, 18 keyword, 22
assignment, 6, 13 kind type parameter, 2
association, 17
letters, 1
assumed-shape arrays, 12
linked chain, 16
automatic arrays, 12
LOGICAL, 3
binary, 2, 20 lower bound, 12, 13
bit inquiry, 22
matrix multiply, 15
bit manipulation, 22
model numbers, 2
blank, 1
modules, 7, 9, 10, 21
CASE construct, 8
named constant, 2
CHARACTER, 3, 20
named operators, 6
comments, 1
numerals, 1
COMPLEX, 2
components, 3 octal, 2, 20
constant expressions, 20 operator, 6
continuation, 1 optional, 22
conversion, 1 overloading, 11
cursor, 23
PARAMETER, 20
DATA, 20 parentheses, 6
defined operators, 7 POINTER, 16
derived data type, 14, 16, 17, 21 pointer
DO construct, 8 s as dynamic aliases, 19
dummy argument, 10, 12, 18 s in expressions and assignments, 17
arguments, 18
edit descriptors, 23
arrays of s, 18
element, 4, 14
functions, 18
elemental, 7
pointer assignment, 16, 17, 19
elemental operation, 13
precedence, 6
explicit interface, 10
PRIVATE, 21
expressions
prompt, 23
initialization of , 20
PUBLIC, 21
specification of , 20
range, 2
formatted files, 23
rank, 12, 18
generic interfaces, 11 REAL, 2
generic names, 11 recursion, 11

heap storage, 13 scope, 21


25
Fortran 90 Tutorial 26

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

You might also like