Next: Input and output
Up: A short summary of
Previous: Loops
  Contents
Subprograms
A subprogram in FORTRAN can be:
A subroutine in FORTRAN is a self-contained program which can be called
from the main program or another subroutine by using a call statement.
The statement call peter
in the main program
calls the subroutine with the name peter
. The subroutine itself
starts with the word subroutine
, then the name of the subroutine,
here peter
,
and then between semicolons the parameters. The subroutine-text
ends with the word end
.
(Remark: in the example above the subroutine peter
has no parameters).
The parameters are used in the sense of `call by reference`.
`Call by value` does not exits in FORTRAN.
The variables in a subroutine which are not in the parameter list (or common
block) are local, so they are only known in the subroutine itself.
Values are transferred from the calling program by using the parameter
mechanism. The same goes for information going the other way
(transferring values from subroutine to calling program).
Subroutines may call other FORTRAN subroutines, but not recursively.
In FORTRAN one also has the FUNCTION subprogram. A function starts with
the word function
,
followed by the name of the function and between semicolons the
formal parameters. A function ends (like a subroutine) with the word
end
.
Within the function subroutine the name of the function must be assigned
a value.
Declarations within a subroutine or function include the declarations of
local variables, variables in the parameter list and the name of the
function. The function also has to be declared in the calling program
(or sub program).
Arrays in the parameter list must be declared in the subprogram as
arrays. For fixed size arrays, the same rules apply as for the main
program. Besides that, variable size arrays are allowed in the
parameter list. The size of the variable size arrays is indicated using
an integer expression which may include variables from the parameter
list.
For variable length arrays one can also use the option *
for the upper index in the last dimension of the array.
(NB. only for formal parameters of functions and subroutines).
Example:
double precision a(3:*), b(*), c(1:n,*), d(4,5,*)
It is important that the index range in the main program are
exactly the same as in the subroutine.
This restriction does not apply to the upper index of the last
dimension.
Example:
program example
implicit none
double precision a(100), x1(100), y1(100), average, v
integer n, i
read *, n
read *, (a(i),i=1,n)
v = average(a,n)
read *, n
read *, (x1(i),i=1,n)
call copy ( x1, y1, n )
end
function average ( a, n )
implicit none
integer n, j
double precision a(n), average, sum
sum = 0d0
do j = 1, n
sum = sum + a(j)
end do
average = sum / n
end
subroutine copy ( x, y, n )
implicit none
integer n, i
double precision x(*), y(*)
do i = 1, n
y(i) = x(i)
end do
end
NB. By default, the FORTRAN compiler does not test array ranges.
Going outside the array index range very often leads to unexpected and
incomprehensible error messages.
A function is called by using its name, together with parameters (if
any) in an expression.
Example:
v = average(a,100)
A subroutine is called with a call statement.
Example:
call copy ( x1, y1, 100 )
Next: Input and output
Up: A short summary of
Previous: Loops
  Contents
Mathieu Pourquie
2001-02-28