CEE15
Introduction to Computing for Civil Engineers
75
Chapter 7
Subprograms
7.1 Designing Functions
Syntax
type
FUNCTION
function-name (arg1, arg2, .
.., argn)
IMPLICIT
NONE
[specification part]
[execution part]
[subprogram part]
END FUNCTION
function-name
§
The first line of a function starts with the keyword
FUNCTION
. Before
FUNCTION
, the
type
gives the type of the function value (
i.e.
,
INTEGER
,
REAL
,
LOGICAL
and
CHARACTER
)
and after
FUNCTION
is the name you assign to that function.
§
Following the
function-name
, there is a pair of parenthesis in which a number of arguments
arg1
,
arg2
, .
..,
argn
are separated with commas. These arguments are referred to as
formal arguments
.
Formal arguments must be variable names and cannot be expressions.
Examples:
§
The following is a function called
Factorial
. It takes only one formal argument
n
and returns an
INTEGER
as its function value.
INTEGER FUNCTION
Factorial(n)
§
The following is a function called
TestSomething
. It takes three formal arguments
a
,
b
and
c
, and
returns a
LOGICAL
value (
i.e.
,
.TRUE.
or
.FALSE.
) as its function value.
LOGICAL FUNCTION
TestSomething(a, b, c)
§
A function could be ended with
END
, or
END FUNCTION,
or
END FUNCTION
followed by
the name of that function.
§
Between
FUNCTION
and
END FUNCTION
, there are the
IMPLICIT NONE
,
specification
part
,
execution part
subprogram part
. These are exactly identical to that of a
PROGRAM
.
§
If a function does not need any formal argument, it can be written as
type
FUNCTION
function-name ()
IMPLICIT
NONE
[specification part]
[execution part]
[subprogram part]
END FUNCTION
function-name
where
arg1
,
arg2
, .
..,
argn
are left out. But, the pair of parenthesis must be there.
Semantics
§
A function is a self-contained unit that receives some "input" from the outside via its formal
arguments, does some computations, and then returns the result with the name of the function.
§
Thus, since the function returns its result via the name of the function, somewhere in the function
there must exist one or more assignment statements like the following: