Vous êtes sur la page 1sur 2

Fortran 90 Reference Card 2 Control Constructs subroutine foo(a,b,c,d,e,x,y)

integer, intent(in) :: a
subroutine definition
read-only dummy variable
(c) 2008 Michael Goerz <goerz@physik.fu-berlin.de> if (expr) action if statement integer, intent(inout) :: b read-write dummy variable
http://www.michaelgoerz.net [name:] if (expr) then if-construct integer, intent(out) :: c write-only dummy variable
block
For a complete reference, I highly recommend real, optional :: d optional named argument
Adams, Brainerd, Martin, Smith, Wagener, Fortran 90 Handbook, Intertext Publications, 1992. else if (expr) then [name]
character (len=*) :: e assumed length string
block
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License. real, dimension (2:, :) :: x assumed-shape dummy array
else [name]
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/ real, dimension (10, *) :: y assumed-size dummy array
block
if (present(d)) ... presence check
end if [name]
1 Data Types select case (number) select-statement
return forced exit
end subroutine foo
1.1 Simple Data Types (entity-oriented declarations) case (:0) everything up to 0 (incl.) call foo(1,2,3,e="s",x=a,y=b) subroutine call
integer(specs) [,attrs] :: i=1 integer (with initialization) block
[real] function f(a,g) function definition
real(specs) [,attrs] :: r real number case (1:2) number is 1 or 2 integer, intent(in) :: a input parameter
complex(specs) [,attrs] :: z complex number block
[real :: f] return type, if not in definition
logical(specs) [,attrs] :: b boolean variable case (3) number is 3 interface explicit interface block
character(specs) [,attrs] :: s string block
real function g(x) define dummy var as function
real, parameter :: c = 2.998 constant declaration case (4:) everything up from 4 (incl.) real, intent(in) :: x
data i,j,k/3*0/ initialize i, j, k to 0 block
end function g
s2=s(2:5); s2=s(:5); s2=s(5:) substring extraction default fall-through case end interface
attributes: parameter, pointer, target, allocatable, block
end function f
dimension, public, private, intent, optional, save, end select
recursive function f(x) ... allow recursion
external, intrinsic outer: do controlled do-loop incr(x) = x + 1 statement function
specs: kind=..., for character: len=... inner: do i=from,to,step counter do-loop interface explicit interface of externals
if (...) cycle inner next iteration
1.1 Derived Data Types if (...) exit outer exit from named loop
interface body ext. subroutine/function specs
type person Define person as derived data end interface
end do inner
character(len=10) :: name type interface generic-name generic interface (overloading)
end do outer
integer :: age interface body external subroutines/functions
do while (expr) do-while loop
end type person module procedure list internal subroutines/functions
block
type(person) :: me instantiate person end interface
end do
me = person(“michael”, 24) constructor interface operator op operator interface
name = me%name access structure component interface body external functions
module procedure list
1.2 Arrays and Matrices 3 Program Structure end interface
internal functions
real, dimension(5) :: v explicit array with index 1..5 program foo main program interface assignment (=) conversion interface
real, dimension(-1:1,3) :: a 2D array, index -1..1, 1..3 use foo, lname => usename used module, with rename interface body external subroutines
integer :: a(-10:5), b(10,20) alternative array declaration use foo2, only: [only-list] selective use module procedure list internal subroutines
real, allocatable :: a(:) alloc. array ("deferred shape") implicit none require variable declaration end interface
a=real(5,5); data a/25*0.0/ initialize 2D array interface; ... explicit interfaces
a=(/1.2,b(2:6,:),3.5/) array constructor end interface
a=(/(I**2), I = 1, N)/) implied-do array constructor specification statements
exec statements
variable/type declarations, etc. 4 Intrinsic Procedures
v = 1/v + a(1:5,5) array expression statements
allocate(a(5),b(2:4),stat=e) stop 'message' terminate program 4.1 Transfer and Conversion Functions
array allocation
contains abs(a) absolute value
1.3 Pointers (avoid!) internal-subprograms aimag(z) imaginary part of complex z
real, pointer :: p declare pointer subroutines, functions
end program foo aint(x, kind), anint(x, kind) to whole number real
real, pointer :: a(:) alloc. array ("deferred shape") module foo dble(a) to double precision
module
real, target :: r define target use foo cmplx(x,y, kind) create x + iy (y optional)
used module
p => r set pointer p to r public :: f1, f2, ... int(a, kind), nint(a, kind) to int (truncated/rounded)
list public subroutines
associated(p, [target]) pointer associated with target? private make private what's not public real(x, kind) to real
nullify(p) associate pointer with NUL interface; ... conj(z) complex conjugate
explicit interfaces
1.4 Operators end interface char(i, kind), achar(i) char of ASCII code (pure 7bit)
.lt. .le. .eq. .ne. .gt. .ge. relational operators specification statements variable/type declarations, etc. ichar(c), iachar(c) ASCII code of character
.not. .and. .or. .eqv. .neqv. logical operators contains logical(l, kind) change kind of logical l
x**(-y) exponentiation internal-subprograms “ module subprgs.” ibits(i, pos, len) extract sequence of bits
'AB'//'CD' string concatenation end module foo transfer(source, mold, size) reinterpret data
4.2 Arrays and Matrices len_trim(s) length of s, w/ trailing blanks 5.3 I/O Specifiers (open)
allocated(a) check if array is allocated scan(s, setd, from_back) search for any char in set iostat=integer-variable save iocode (error) to variable
lbound(a, dim), ubound(a,dim) lowest/highest index in array verify(s, set, from_back) check for presence of set-chars
err=label label to jump to on error
shape(a) shape (dimensions) of array len(string) length of string file='filename' name of file to open
size(array, dim) extent of array along dim repeat(string, n) concat n copies of string status='old' 'new' 'replace' status of input file
all(mask, dim), any(mask, dim) check boolean array 4.6 Bit Functions (on integers) 'scratch' 'unknown'
count(mask, dim) number of true elements btest(i,pos) test bit of integer value access='sequential' 'direct' access method
maxval(a,d,m), minval(a,d,m) find max/min in masked array iand(i,j), ieor(i,j), ior(i,j) and, xor, or of bit in 2 integers form='formatted' 'unformatted' formatted/unformatted I/O
product(a, dim, mask) product along masked dimen. ibclr(i,pos), ibset(i, pos) set bit of integer to 0 / 1 recl=integer length of record
sum(array, dim, mask) sum along masked dimension ishft(i, sh), ishftc(i, sh, s) shift bits in i blank='null' 'zero' ignore blanks/treat them as 0
merge(tsource, fsource, mask) combine arrays as mask says not(i) bit-reverse integer position='asis''rewind' position, if sequential I/O
pack(array, mask, vector) packs masked array into vect.
4.7 Misc Intrinsic Subroutines 'append'
unpack(vector, mask , field) unpack vect. into masked field
date_and_time(d, t, z, v) put current time in d,t,z,v action='read' 'write' read/write mode
spread(source, dim, n) extend source array into dim.
mvbits(f, fpos, len, t, tpos) 'readwrite'
copy bits between int vars
reshape(src,shape,pad,order) make array of shape from src
random_number(harvest) delim='quote' 'apostrophe' delimiter for char constants
fill harvest randomly
cshift(a,s,d),eoshift(a,s,b,d) (circular) shift 'none'
random_seed(size, put, get) restart/query random generator
transpose(matrix) transpose a matrix pad='yes' 'no' pad with blanks
system_clock(c, cr, cm) get processor clock info
maxloc(a,mask), minloc(a,mask) find pos. of max/min in array close-specifiers: iostat, err, status='keep' 'delete'
4.3 Computation Functions inquire-specifiers: access, action, blank, delim, direct,
ceiling(a), floor(a) to next higher/lower int
5 Input/Output exist, form, formatted, iostat, name, named, nextrec,
conj(z) complex conjugate 5.1 Format Statements number, opened, pad, position, read, readwrite, recl,
dim(x,y) max(x-y, 0) fmt = "(F10.3, A, ES14.7)" format string sequential, unformatted, write, iolength
max(a1, a2, a3..), min(a1, ..) maximum/minimum Iw Iw.m integer form backspace-, endfile-, rewind-specifiers: iostat, err
dprod(a,b) dp product of sp a, b Bw.m Ow.m Zw.m binary, octal, hex integer form 5.4 Data-Transfer Specifiers (read, write)
mod(a,p) a mod p Fw.d decimal form real format
iostat=integer-variable save iocode (error) to variable
modulo(a,p) modulo with sign of a/p Ew.d exponential form (0.12..E-11)
advance='yes' 'no' (non-)advancing data transfer
sign(a,b) make sign of a = sign of b Ew.dEe specified exponent length
err=label label to jump to on error
matmul(m1, m2) matrix multiplication ESw.d ESw.dEe scientific form (1.2...E-10)
end=label label to jump to on end of file
dot_product(a,b) dot product of vectors ENw.d ENw.dEe engineer. form (123.4...E-12)
eor=label label for end of record
more: sin, cos, tan, acos, asin, atan, atan2, sinh, Gw.d generalized form
rec=integer record number to read or write
cosh, tanh, exp, log, log10, sqrt Gw.dEe generalized exponent form
size=integer-variable number of characters read
Lw logical format (T, F)
4.4 Numeric Inquiry and Manipulation Functions A Aw characters format
kind(x) kind-parameter of variable x nX horizontal positioning (skip)
digits(x) significant digits in model Tc TLc TRc move (absolute, left, right)
bit_size(i) number of bits for int in model r/
vert. positioning (skip lines)
epsilon(x) small pos. number in model r(...) grouping / repetition
huge(x) largest number in model : format scanning control
minexponent(x) smallest exponent in model S SP SS sign control
maxexponent(x) largest exponent in model BN BZ blank control (blanks as zeros)
precision(x) decimal precision for reals in
w full length, m minimum digits, d decimal places, e exponent length,
radix(x) base of the model
n positions to skip, c positions to move, r repetitions
range(x) dec. exponent range in model
tiny(x) smallest positive number 5.2 Reading from and Writing to Files
exponent(x) exponent part of x in model call getarg(2, var) put 2nd CLI-argument in var
fraction(x) fractional part of x in model print '(i10)', 2 print to stdout with format
nearest(x) nearest machine number print *, “Hello World” list-directed I/O
rrspacing(x) reciprocal of relative spacing write(unit, fmt, spec) list write list to unit
scale(x,i) x b**i read(unit, fmt, spec) list read list from unit
set_exponent(x,i) x b**(i-e) open(unit, specifiers) open file
spacing(x) absolute spacing of model close(unit, specifiers) close file
inquire(unit, spec) inquiry by unit
4.5 String Functions inquire(file=filename, spec) inquiry by filename
lge(s1,s2), lgt, lle, llt string comparison inquire(iolength=iol) outlist inquiry by output item list
adjustl(s), adjustr(s) left- or right-justify string backspace(unit, spec) go back one record
index(s, sub, from_back) find substr. in string (or 0) endfile(unit, spec) write eof record
trim(s) s without trailing blanks rewind(unit, spec) jump to beginning of file