C++ Notes: Function Exercises 1

Name: _________________________________

For the following questions, assume that the proper includes and function prototype declarations have been made before these function definitions:
   //--------------------- f
   int f(int a, int b) {
       if (a > b) {
          result = a;
       }else{
          result = b;
       }
       return result;
   }//endfunction f

   //--------------------- g
   int g(int c) {
       int sum = 0;
       for (int i=1; i<=c; i++) {
          sum += i;
       }
       return sum;
   }//endfunction g
  
   //---------------------- m
   int m(int a) {
       if (a < 0) {
           return -1;
       }else if (a == 0) {
           return 0;
       }else{
           return 1;
       }
   }

   //----------------------- e
   int e(int& a, int& b, int c) {
      a = b;
      b = c;
      return 1;
   }

What is the output from each of these code fragments? Write ILLEGAL if the code is not correct. Hint: there are three illegal fragments.
  1. _____________ cout << f(2, 1);
  2. _____________ cout << f(f(5,2), f(-6,8));
  3. _____________ cout << g(3) + g(4);
  4. _____________ cout << g(g(g(2)));
  5. _____________ cout << m(6%5 - 8);
  6. _____________ cout << f(g(2), m(f(-3)));
  7. _____________ int x=1; int y=2; cout << e(x, y, 3);
  8. _____________ int x=1; int y=2; e(x, y, 3); cout << x;
  9. _____________ int x=1; int y=2; e(3, y, x); cout << x;
  10. _____________ int x=1; int y=2; cout << e(x+1, y+1, 3+1);