Module 6. Functions Exercises

1. The C++ program below has a function sumup that is defined after
the call to sumup in function main.  Insert a line after the #include
so that it is an appropriate function prototype for function sumup.

   #include <iostream.h>

   void main ()
   {
    float val;
    float amount;
    int n;
    cin >> val >> n;
    sumup (amount, val, n);
    cout << amount << endl;
   }

   void sumup (float& sum, float x, int n)
   {
    int i;
    sum = 0.0;
    for (i=1; i<=n; i++)
      {
       sum = sum + i * x;
      }
   }

2. What is wrong with the following function?
float resolve (int a, float b, char c, float d)

{
 char e;
 float c;
 ...
}

(a) All function parameters a, b, c, and d must be of the same type
(b) No local variables can be declared in function resolve
(c) The function parameter c cannot also be declared as a local variable
(d) The function parameter c can only be declared as a local
        variable if type char is used

3. Which of the following is a proper function prototype for function
resolve in question 2 above?  (Hint -- Look closely.)
(a) float resolve (int, float, char, float)
(b) float resolve (int, float, char, float);
(c) resolve (int, float, char, float)
(d) void resolve (int, float, char, float);

4. What return type is used if a function returns no value?
(a) float
(b) int
(c) prototype 
(d) void

5. Actual arguments and function parameters must be...?
(a) same number
(b) same number and same types
(c) same types
(d) same number and different types

6. Which of the following best describes what this function does?

void increase (int number)
{
 ++number;
 return;
}

(a) changes both the local variable number AND the
        value of the actual parameter in the calling program 
(b) changes the local variable number, but NOT the
        value of the actual parameter in the calling program 
(c) gets an error message from the C++ compiler since it really
        should start with
        void increase (int& number)
(d) is an example of call-by-reference