|
|
|
|
|
|
|
(text box continued from previous page) |
|
|
|
|
|
|
|
|
Boolean value-returning functions (and variables) are often named using adjectives or phrases beginning with Is. Here are a few examples: |
|
|
|
| |
|
|
|
|
while (Valid(m, n))
if (Odd(n))
if (IsTriangle(s1, s2, s3)) |
|
|
|
| |
|
|
|
|
When you are choosing a name for a value-returning function, try to stick with nouns or adjectives so that the name suggests a value, not a command to the computer. |
|
|
|
|
|
|
|
|
|
Interface Design and Side Effects |
|
|
|
|
|
|
|
|
The interface to a value-returning function is designed in much the same way as for a void function. We simply write down a list of what the function needs and what it must return. Because value-returning functions return only one value, there is only one item labeled outgoing in the list (the function return value). Everything else in the list is labeled incoming, and there aren't any incoming/outgoing parameters. |
|
|
|
|
|
|
|
|
Returning more than one value from a value-returning function (by modifying the actual parameters) is an unwanted side effect and should be avoided. If your interface design calls for multiple values to be returned or for the values of actual parameters to be changed, then you should use a void function instead of a value-returning function. |
|
|
|
|
|
|
|
|
A rule of thumb is never to use reference parameters in the formal parameter list of a value-returning function, but to use value parameters exclusively. Let's look at an example that demonstrates the importance of this rule. Suppose we define the following function: |
|
|
|
|
|
|
|
|
int SideEffect( int& n )
{
int result = n * n;
n++; // Side effect
return result;
} |
|
|
|
|
|
|
|
|
This function returns the square of its incoming value, but it also increments the actual parameter before returning. Now suppose we call this function with the following statement: |
|
|
|
|
|