|
Page 4 of 5
PRE and POST
These comment headers are assertions
associated with subroutines. In C++ they are used to establish the
conditions that are expected at the entry and exit points of a
function. Before the assertion notation was proposed some
programmers used the terms ON ENTRY: and ON EXIT:
The PRE: assertion describes the elements
(usually variables and constants) that must be supplied by the
caller and the POST: assertion describes the state of computation
at the moment the function concludes its execution. Thus, the PRE:
and POST: conditions represent the terms of a contract between
caller and function, stating that if the caller ensures that the
PRE: assertion is true at call time, the function guarantees that
the POST: assertion is satisfied at return time. For example, the
following function named Swap() exchanges the values in two integer
variables.
void Swap( int& var1, int&
var2)
// PRE: Assigned var1 && var2
// POST: var1 == var2 && var2 ==
var1
{
int temp = var1;
var1 = var2;
vat 2 = temp;
return;
}
FCTVAL
This assertion is also used in the context
of subroutines to indicate the returned value. In C++ FCTVAL
represents the value returned by a function and associated with the
function name. Therefore it should not be used to represent
variables modified by the function. For example, the following
trivial function returns the average of three real numbers passed
by the caller.
float Avrg( float num1, float num2, float
num3)
// PRE: Assigned num1 && num2
&& num3
// num1 + num2 + num3
// POST: FCTVAL == ..........
// 3
{
return ((num1 + num2 + num3) / 3));
}
|