Main Page

Previous Page
Next Page

[Page 311]

Self-Review Exercises


Answer each of the following:

  1. Program components in C++ are called ________ and ________.

  2. A function is invoked with a(n) ________.

  3. A variable that is known only within the function in which it is defined is called a(n) ________.

  4. The ________ statement in a called function passes the value of an expression back to the calling function.

  5. The keyword ________ is used in a function header to indicate that a function does not return a value or to indicate that a function contains no parameters.

  6. The ________ of an identifier is the portion of the program in which the identifier can be used.

  7. The three ways to return control from a called function to a caller are ________, ________ and ________.

  8. A(n)________ allows the compiler to check the number, types and order of the arguments passed to a function.

  9. Function ________ is used to produce random numbers.

  10. Function ________ is used to set the random number seed to randomize a program.

  11. The storage-class specifiers are mutable, ________, ________, ________ and ________.

  12. Variables declared in a block or in the parameter list of a function are assumed to be of storage class ________ unless specified otherwise.

  13. Storage-class specifier ________ is a recommendation to the compiler to store a variable in one of the computer's registers.

  14. A variable declared outside any block or function is a(n) ________ variable.

  15. For a local variable in a function to retain its value between calls to the function, it must be declared with the ________ storage-class specifier.

  16. The six possible scopes of an identifier are ________, ________, ________, ________, ________ and ________.

  17. A function that calls itself either directly or indirectly (i.e., through another function) is a(n) ________ function.

  18. A recursive function typically has two components: One that provides a means for the recursion to terminate by testing for a(n) ________ case and one that expresses the problem as a recursive call for a slightly simpler problem than the original call.

  19. In C++, it is possible to have various functions with the same name that operate on different types or numbers of arguments. This is called function ________.

  20. The ________ enables access to a global variable with the same name as a variable in the current scope.

  21. The ________ qualifier is used to declare read-only variables.

  22. A function ________ enables a single function to be defined to perform a task on many different data types.


For the program in Fig. 6.40, state the scope (either function scope, file scope, block scope or function-prototype scope) of each of the following elements:

  1. The variable x in main.

  2. The variable y in cube.

  3. The function cube.

  4. The function main.

  5. The function prototype for cube.

  6. The identifier y in the function prototype for cube.

Figure 6.40. Program for Exercise 6.2
(This item is displayed on page 312 in the print version)

 1  // Exercise 6.2: Ex06_02.cpp
 2  #include <iostream>
 3  using std::cout;
 4  using std::endl;
 6  int cube( int y ); // function prototype
 8  int main()
 9  {
10     int x;
12     for ( x = 1; x <= 10; x++ ) // loop 10 times
13        cout << cube( x ) << endl; // calculate cube of x and output results
15     return 0; // indicates successful termination
16  } // end main
18  // definition of function cube
19  int cube( int y )
20  {
21     return y * y * y;
22  } // end function cube

[Page 312]

Write a program that tests whether the examples of the math library function calls shown in Fig. 6.2 actually produce the indicated results.


Give the function header for each of the following functions:

  1. Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result.

  2. Function smallest that takes three integers, x, y and z, and returns an integer.

  3. Function instructions that does not receive any arguments and does not return a value. [Note: Such functions are commonly used to display instructions to a user.]

  4. Function intToDouble that takes an integer argument, number, and returns a double-precision, floating-point result.


Give the function prototype for each of the following:

  1. The function described in Exercise 6.4(a).

  2. The function described in Exercise 6.4(b).

  3. The function described in Exercise 6.4(c).

  4. The function described in Exercise 6.4(d).


Write a declaration for each of the following:

  1. Integer count that should be maintained in a register. Initialize count to 0.

  2. Double-precision, floating-point variable lastVal that is to retain its value between calls to the function in which it is defined.


Find the error in each of the following program segments, and explain how the error can be corrected (see also Exercise 6.53):

    [Page 313]
  1. int g( void )
        cout << "Inside function g" << endl;
        int h( void )
           cout << "Inside function h" << endl;

  2. int sum( int x, int y )
        int result;
        result = x + y;

  3. int sum( int n )
        if ( n == 0 )
           return 0;
           n + sum( n - 1 );

  4. void f ( double a);
        float a;
        cout << a << endl;

  5. void product( void )
        int a;
        int b;
        int c;
        int result;
        cout << "Enter three integers: ";
        cin >> a >> b >> c;
        result = a * b * c;
        cout << "Result is " << result;
        return result;


Why would a function prototype contain a parameter type declaration such as double &?


(True/False) All arguments to function calls in C++ are passed by value.


Write a complete program that prompts the user for the radius of a sphere, and calculates and prints the volume of that sphere. Use an inline function sphereVolume that returns the result of the following expression: ( 4.0 / 3.0 ) * 3.14159 * pow( radius, 3 ).

Previous Page
Next Page