Fortran is the most widely used programming language for numerical and
high-performance computing. Fortran 90 is the current version which adds many
important features to Fortran 77, e.g., dynamic memory allocation, array
processing features, structured data types, modules and pointers. Note that
all Fortran 77 commands remain valid, but some (like GOTO,
COMMON) are made obsolete by new Fortran 90 features.
Fortran 90 is available on WAM and Glue Sun and DEC Alpha machines and Math
department Sun machines.
To compile a program type f90 myprogram.f90 . To run the resulting
program type a.out .
This program illustrates the most important Fortan 90 commands:
! this module can be placed in separate file and compiled separately
module vectorfun
implicit none
contains
function length(v)
! compute the euclidian length of a vector
real :: length, v(:), s
integer :: n, i
n = size(v)
s = 0.
do i=1,n
s = s + v(i)**2
end do
length=sqrt(s)
end function
end module
! the main program can be placed in a separate file and compiled separately
program test
use vectorfun ! this makes all declarations and definitions of module available
implicit none
integer :: i,n
real :: r
real, allocatable :: x(:)
print *,'n ='
read *, n
allocate(x(n))
do i=1,n
print *,'x(',i,') ='
read *, x(i)
end do
r=length(x)
print *, 'length =',r
end program
Fortran 90 can use a free format which is not restricted to certain
columns.
-
Fortran 90 does not distinguish between lower and upper case
characters, except between quotes (like
'This' or
"This")
-
! indicates that the rest of the line is a comment
-
& at the end of a line indicates that it is continued in the next line
I strongly recommend that you declare all variables explicitly and put the
statement implicit none at the beginning of your programs and
modules.
-
available data types for numbers are integer and
real, or, more generally, integer(q) and real(q)
where q is an integer constant or parameter. On Sun and DEC Alpha,
for integers q can be 1, 2, 4 (default), 8. For reals, q can
be 4 (default), 8 (same as double precision), 16 (``quad precision'',
DEC Alpha only). As other machines use other values and other precisions one
can also specify q in the form
integer,parameter::q=selected_real_kind(10,99) which automatically
chooses q such that real(q) has at least 10 decimal digits
of precision, and a range of at least 10-99 to 1099 (example program).
-
integer constants are of the form 1234, real constants are of
the form 1234. or 1.234e3. General real constants are of the
form
1234._q or 1.234e3_q with q as above.
-
arithmetic operators like +, -, *,
/, ** and intrinsic functions like min,
max, abs, sqrt, sin, cos,
tan, asin, acos, atan, exp,
log,... return a result of the same type as the argument(s).
Functions like sqrt, sin,...cannot be used with integer
arguments.
-
This means that 1/5 gives the integer 0, 1./5. gives 0.2 in
single precision, and
1._8/5._8 (on DEC Alpha) gives 0.2 in
double precision. sqrt(4) will give an error.
-
Arrays of the above types can also be used: integer::a(10,10)
declares an array of the integer variables a(1,1),...,
a(10,10). real::b(0:10) declares an array of the real
variables b(0),...,b(10).
-
Arrays of variable size can be declared using x(:) or
a(:,:). If they are arguments of a function or subroutine they will
have the size of the array which is the actual argument. If they are not
arguments then they have to be declared allocatable, and statements
of the form allocate(a(m,n)) must allocate memory before the arrays
are used. To free the memory use deallocate(a).
-
Array processing: All of the above arithmetic operations and intrinsic
functions can be applied to an array and are processed element by element.
E.g. for two arrays x, y of the same shape we can write
y=y+sin(x). Intrinsic functions which can be applied to arrays are
size, sum, maxval, minval,
dot_product, matmul. Subarrays can be denoted using a
colon notation, e.g. a(2:4,2:3) gives the 3 by 2 submatrix of of the
elements a(i,j) where i=2,3,4 and j=2,3.
a(:,2) is a one dimensional array containing the second column of the
matrix a.
-
if (condition) then
statements
else
statements
end if
Here condition uses the operators ==, /=,
<, >, <=, >= for =,
, <, >, , . Note that the parentheses around condition cannot be
omitted. The else part may be omitted.
-
do var=start,end
statements
end do
Here var must be a variable of type integer. If start>end
the statements are not executed. To count down, use do
var=start,end,-1.
-
do
statements
if (condition) exit
statements
end do
This can be used for ``While'' or ``Repeat-Until'' loops. The exit
statement jumps to the statement after the next end do statement.
-
Subroutines begin with subroutine name(arg1,
arg2,...), then the types of all arguments and local variables
should be declared. Subroutines end with end subroutine. Subroutines
are called from another routine with the command call
name(arg1, arg2,...). For recursive subroutines use
recursive subroutine name(arg1, arg2,...).
-
Functions begin with function name(arg1,
arg2,...), then the types of name, all arguments and local
variables should be declared. Note that the type of name has also to be
declared in the calling program unless the function is in a module. In the
body of the function a value must be assigned to the variable name.
Functions end with the end function. For recursive functions
use recursive function name(arg1,arg2,...)
result(resname) and assign a value to the variable resname
in the body of the function.
-
All arguments are passed using ``call by reference'' (like VAR arg in
Pascal or &arg in C++). Changing the value of an argument in the
subroutine changes the value of the corresponding variable in the calling
program. Note that (unlike Fortran 77) for array arguments the dimensions are
also passed to the subroutine (example)
-
I recommend that you put your functions and subroutines in a module and
use this module in the main program. If you do not do this,
the names of functions must be declared in the main program, and in
some cases interface statements must be used in the main
program. Using modules allows complete typechecking at compile time, even if
modules are compiled separately.
-
A module begins with module name, then variables can be
declared. After a contains statement subroutines and functions can be
defined. The module ends with end module. The statement use
name at the beginning of the main program, a subroutine or a
function makes all variables, subroutines and functions of the module
available. If the module is in the same file it must occur before it is
used.
T. von Petersdorff , 