< previous page page_374 next page >

Page 374
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
temporary variable whose address is passed to the function, and the results may not be what you expect. With pass-by-value, implicit type coercion takes place.
8. Remember that a reference parameter requires a variable as an actual parameter, whereas a value parameter can have any expression that supplies a value of the same data type (except as noted in hint 7) as an actual parameter.
9. Become familiar with all the tools available to you when you're trying to locate the sources of bugsthe algorithm walk-through, hand tracing, the system's debugger program, the assert function, and debug output statements.
Summary
C++ allows us to write programs in modules expressed as functions. The structure of a program, therefore, can parallel its top-down design even when the program is complicated. To make your main function look exactly like Level 0 of your top-down design, simply write each lower-level module as a function. The main function then executes these other functions in logical sequence.
An important means of communication between two functions is the use of two parameter lists: the formal parameter list (which includes the data type of each identifier) in the function heading, and the actual parameter list in the calling code. The items in these lists must agree in number and position, and they should agree in data type.
Part of the top-down design process involves determining what data must be received by a lower-level module and what information must be returned from it. The names of these data values, together with the precondition and postcondition of a module, define its interface. The names of the data values become the formal parameter list, and the module name becomes the name of the function. With void functions, a call to the function is accomplished by writing the function's name as a statement, enclosing the appropriate actual parameters in parentheses.
C++ has two kinds of formal parameters: reference and value. Reference parameters have data types ending in &s
in the formal parameter list, whereas value parameters do not. Parameters that return values from a function should be reference parameters; all others should be value parameters. This minimizes the risk of errors, because only a copy of the value of an actual parameter is passed to a value parameter, and thus the original value cannot be changed.
In addition to the variables declared in its formal parameter list, a function may have local variables declared within it. These variables are accessi-

 
< previous page page_374 next page >