< previous page page_450 next page >

Page 450
ry remains allocated for the duration of the program's execution). By default, local variables have automatic lifetime (memory is allocated and deallocated at block entry and block exit). A local variable may be given static lifetime by using the word static in its declaration. This variable has the lifetime of a global variable but the scope of a local variable.
C++ allows a variable to be initialized in its declaration. For a static variable, the initialization occurs once onlywhen control first reaches its declaration. An automatic variable is initialized each time control reaches the declaration.
C++ provides two kinds of subprograms, void functions and valuereturning functions, for us to use. A value-returning function is called from within an expression and returns a single result that is used in the evaluation of the expression. For the function value to be returned, the last statement executed by the function must be a return statement containing an expression of the appropriate data type.
All the scope rules, as well as the rules about reference and value parameters, apply to both void functions and value-returning functions. It is considered poor programming practice, however, to use reference parameters in a value-returning function declaration. Doing so increases the potential for side effects. (An exception is when I/O stream variables are passed as parameters. Other exceptions are noted in later chapters.)
We can use stubs and drivers to test functions in isolation from the rest of a program. They are particularly useful in the context of team-programming projects.
Quick Check
1. a. How can you tell if a variable that is referenced inside a function is local or global? (pp. 390395)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Where are local variables declared? (pp. 390395)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
c. When does the scope of an identifier declared in block A exclude a block nested within block A? (pp. 390395)
2. A program consists of two functions, main and DoCalc. A variable x is declared outside both functions. DoCalc declares two variables, a and b, within its body; b is declared as static. In what function(s) are each of a, b, and x visible, and what is the lifetime of each variable? (pp. 390395, 397398)
3. Why should you use value parameters whenever possible? Why should you avoid the use of global variables? (pp. 400404, 415416)
4. For each of the following, decide whether a value-returning function or a void function is the most appropriate implementation. (pp. 404417)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. Selecting the larger of two values for further processing in an expression.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Printing a paycheck.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
c. Computing the area of a hexagon.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
d. Testing whether an incoming value is valid and returning TRUE if it is.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
e. Computing the two roots of a quadratic equation.

 
< previous page page_450 next page >