Go backward to Specifying concepts
Go up to User documentation of generic software components
Go forward to When it may be necessary to explain design decisions
Specifying functions
Consider the STL generic
merge
function. Do you see how it can it be used more generally
than the SGI description implies?
An effective way to describe the
input-output relation computed by a function is in terms of
preconditions, postconditions, and effects.
- precondition:
- a predicate that states what condition (relation
of input variables) must be satisfied by any call of the function;
sometimes called a requires clause.
- postcondition:
- a predicate that states what condition (relation
of input and output variables) that is satisfied after the function
call is completed, provided the precondition is satisfied;
sometimes called an ensures clause.
- effects:
- a list of changes the function makes on variables other
than those considered to be outputs of the function; sometimes called
a side-effects or modifies clause.
A function may throw an exception, in which it does not return in a
normal manner and its input-output relation as described in
the above terms does not apply. Any condition under which a
function throws an exception is in general unrelated to the
function's precondition: - If the precondition is violated, an exception is not necessarily
thrown. [Give an example.]
- Even if the precondition is satisfied, an exception may
be thrown. [Give an example.]