Module 7. Function Defaults, Recursion, Overloading Exercises

1. Create a function prototype for sumup that specifies the default
1.0 for x and 1 for n.

   #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. Overload the function name manip in the program below.  The new
function manip has two float parameters num1 and num2 that are both
passed by value.  This manip function returns the quotient num1 / num2
of those two float values.  Don't forget to include a function
prototype.

   #include <iostream.h>

   int manip (int, int);

   void main ()
   {
    int x = 3;
    int y = 7;
    int z;
    float a = 6.8;
    float b = 13.6;
    int c;
    z = manip (x,y);
    c = manip (a,b);
    cout << z << c << endl;
   }
   int manip (int num1, int num2)
   {
    return (num1 * num2);
   }

3. Which of the following best describes the statement 
z = sum (5.2); below?

float sum (float = 1.0, float = 0.0);
...
z = sum (5.2);

(a) equivalent to z = sum (1.0, 0.0);  
(b) equivalent to z = sum (5.2, 0.0);  
(c) equivalent to z = sum (5.2, 1.0);  
(d) leads to a compile-time error -- not enough arguments to
        function sum

4. What is (are) the major characteristic(s) of a recursive function?
(a) a function that calls itself every time it is invoked, and has
        NO way to end without calling itself 
(b) a function that has a loop and does NOT call itself
(c) a function that has no loops
(d) a function that usually calls itself, but has a way to end without
        calling itself 

5. What is required in Function Overloading?
(a) Functions must have different number of parameters
(b) Functions must have different number of parameters or at least one
parameter must be different type
(c) Functions must have different return types
(d) Functions must have same number of parameters but at least one
parameter must be different type

6. Given the following function prototypes...

int suspend (int, int);
float suspend (int, int=30, int=25, int=53);

...which of the following function calls leads to an ambiguity?
(a) suspend (3);
(b) suspend (3,5);
(c) suspend (3,5,7);
(d) suspend (3,5,7,9);