|
|
|
|
|
|
|
temporaryInt = firstInt;
firstInt = secondInt;
secondInt = temporaryInt;
} |
|
|
|
| |
|
|
|
|
Function Preconditions and Postconditions |
|
|
|
| |
|
|
|
|
Preconditions and postconditions, when well written, are a concise but accurate description of the behavior of a function. A person reading your program should be able to see at a glance how to use the function by looking only at its interface (the function heading and the precondition and postcondition). The reader should never have to look into the code of the function body to understand the purpose of the function or how to use it. |
|
|
|
| |
|
|
|
|
A function interface describes what the function does, not the details of how it does it. For this reason, the postcondition should mention (by name) each outgoing parameter and its value but should not mention any local variables. Local variables are implementation details; they are irrelevant to the function's interface. |
|
|
|
|
|
|
|
|
|
Documenting the Direction of Data Flow |
|
|
|
|
|
|
|
|
Another helpful piece of documentation in a function interface is the direction of data flow for each parameter in the parameter list. Data flow is the flow of information between the function and its caller. We said earlier that each parameter can be classified as an incoming parameter, an outgoing parameter, or an incoming/outgoing parameter. (Some people refer to these as input parameters, output parameters, and input/output parameters.) |
|
|
|
 |
|
 |
|
|
Data Flow The flow of information from the calling code to a function and from the function back to the calling code. |
|
|
|
|
|
|
|
|
For an incoming parameter, the direction of data flow is one-wayinto the function. The function inspects and uses the current value of the parameter but does not modify it. In the function heading, we attach the comment |
|
|
|
|
|