|
|
|
|
|
|
|
deposit results into the caller's actual parameters is to have the addresses of those parameters. For emphasis, we repeat the following table from Chapter 7. |
|
|
|
|
| Data Flow for a Parameter | Parameter-Passing Mechanism | | Incoming | Pass-by-value | | Outgoing | Pass-by-reference | | Incoming/outgoing | Pass-by-reference |
|
|
|
|
|
|
As we said in the last chapter, there are exceptions to the guidelines in this table. C++ requires that I/O stream variables be passed by reference because of the way streams and files are implemented. We encounter another exception in Chapter 11. |
|
|
|
|
|
|
|
|
Sometimes it is tempting to skip the interface design step when writing a function, letting it communicate with other functions by referencing global variables. Don't! Without the interface design step, you would actually be creating a poorly structured and undocumented interface. Except in welljustified circumstances, the use of global variables is a poor programming practice that can lead to program bugs. These bugs are extremely hard to locate and usually take the form of unwanted side effects. |
|
|
|
|
|
|
|
|
Suppose you made a call to the sqrt library function in your program: |
|
|
|
|
|
|
|
|
You expect that the call to sqrt will compute the square root of the variable x. You'd be surprised if sqrt also changed the value of your variable x because sqrt, by definition, does not make such changes. This would be an example of an unexpected and unwanted side effect. |
|
|
|
 |
|
 |
|
|
Side Effect Any effect of one function on another that is not a part of the explicitly defined interface between them. |
|
|
|
|
|
|
|
|
Side effects are sometimes caused by a combination of reference parameters and careless coding in a function. Perhaps an assignment statement in |
|
|
|
|
|