< previous page page_357 next page >

Page 357
to the declaration of the formal parameter. (Remember that C++ comments come in two forms. The first, which we use most often, starts with two slashes and extends to the end of the line. The second form encloses a comment between /* and */ and allows us to embed a comment within a line of code.) Here is the PrintAverage function with comments added to the formal parameter declarations:
void PrintAverage( /* in */ float sum,
                   /* in */ 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;
}
Pass-by-value is appropriate for each parameter that is incoming only. As you can see in the function body, PrintAverage does not modify the values of the parameters sum and count. It merely uses their current values. The direction of data flow is one-wayinto the function.
The data flow for an outgoing parameter is one-wayout of the function. The function produces a new value for the parameter without using the old value in any way. The comment /* out */ identifies an outgoing parameter. Here we've added comments to the Get2Ints function heading:
void Get2Ints(  /* out */ int& int1,
                /* out */ int& int2  )
Pass-by-reference must be used for an outgoing parameter. If you look back at the body of Get2Ints, you'll see that the function stores new values into the two variables (by means of the input statement), replacing whatever values they originally contained.
Finally, the data flow for an incoming/outgoing parameter is two-way into and out of the function. The function uses the old value and also produces a new value for the parameter. We use /* inout */ to document this two-way direction of data flow. Here is an example of a function that uses two parameters, one of them incoming only and the other one incoming/outgoing:

 
< previous page page_357 next page >