|
|
|
|
|
|
|
5. What would the heading for a value-returning function named Min look like if it had two float parameters, num1 and num2, and returned a float result? (pp.404417) |
|
|
|
|
|
|
|
|
6. What would a call to Min look like if the actual parameters were a variable named deductions and the literal 2000.0? (pp. 404417) |
|
|
|
|
|
|
|
|
Answers 1. a. If the variable is not declared in either the body of the function or its formal parameter list, then the reference is global. b. Local variables are declared within a block (compound statement). c. When the nested block declares an identifier with the same name. 2. x is visible to both functions, but a and b are visible only within DoCalc. x and b are static variables; once memory is allocated to them, they are alive until the program terminates. a is an automatic variable; it is alive only while DoCalc is executing. 3. Both using value parameters and avoiding global variables will minimize side effects. Also, pass-by-value allows the actual parameters to be arbitrary expressions. 4. a. Value-returning function b. Void function c. Value-returning function d. Value-returning function e. Void function |
|
|
|
|
|
|
|
|
5. float Min( float num1,
float num2 )
6. smaller = Min(deductions, 2000.0); |
|
|
|
|
|
|
|
|
Exam Preparation Exercises |
|
|
|
|
|
|
|
|
1. If a function contains a locally declared variable with the same name as a global variable, no confusion results because references to variables in functions are first interpreted as references to local variables. (True or False?) |
|
|
|
|
|
|
|
|
2. Variables declared at the beginning of a block are accessible to all statements in that block, including those in nested blocks (assuming the nested blocks don't declare local variables with the same names). (True or False?) |
|
|
|
|
|
|
|
|
3. Define the following terms. |
|
|
|
|
| | | | | |
|
|
|
|
name precedence (name hiding) |
|
|
|
|
|
|
|
|
|
|
4. What is the output of the following C++ program? (This program is an example of poor interface design practices.) |
|
|
|
 |
|
|
|
|
#include <iostream.h>
void DoGlobal( );
void DoLocal();
void DoReference( int& );
void DoValue( int );
int x;
int main( )
{
|
|
|
|
|
|