< previous page page_354 next page >

Page 354
3. Incoming/outgoing valuesvalues the caller has that the function changes (receives and returns).
Decide which identifiers inside the module match the values in this list. These identifiers become the variables in the formal parameter list for the function. Then the formal parameters are declared in the function heading. All other variables that the function needs are local and must be declared within the body of the function. This process may be repeated for all the modules at each level.
Let's look more closely at designing the interface. First we examine function preconditions and postconditions. After that, we consider in more detail the notion of incoming, outgoing, and incoming/outgoing parameters.
Writing Assertions as Program Comments
We have been writing module preconditions and postconditions as informal, English-language assertions. From now on, we include preconditions and postconditions as comments to document the interfaces of C++ functions. Here's an example:
void PrintAverage( float sum,
                   int   count )

// Precondition:
//     sum is assigned && count > 0
// Postcondition:
//     The average sum/count has been output on one line

{
    cout << Average is  << sum / float(count) << endl;
}
The precondition is an assertion describing everything that the function requires to be true at the moment the caller invokes the function. The postcondition describes the state of the program at the moment the function finishes executing.
You can think of the precondition and postcondition as a contract. The contract states that if the precondition is true at function entry, then the postcondition must be true at function exit. The caller is responsible for ensuring the precondition, and the function code must ensure the postcondition. If the caller fails to satisfy its part of the contract (the precondition), the contract is off; the function cannot guarantee that the postcondition will be true.
Above, the precondition warns the caller to make sure that sum has been assigned a meaningful value and to be sure that count is positive. If this precondition is true, the function guarantees it will satisfy the postcondition. If count isn't positive when PrintAverage is invoked, the effect of the function

 
< previous page page_354 next page >