7B. Calling Your Functions

You should feel free to use your functions whenever you need them. Use them exactly the way you would if they were library functions. There is really no difference between your functions and library functions.

Example: largestOfFive

For example, you can compute the largest of five numbers by doing two calls to function largest.
  // largestOfFive(u, v, w, x, y) returns the largest
  // of u, v, w, x and y.

  double largestOfFive(double u, double v, double w,
                       double x, double y)
  {
    return largest(u, v, largest(w, x, y));
  }

As you can see, the parameters passed to largest can be any expressions. They are not required to be variables called x, y and z.


The form of a function call

After seeing a type in front of each argument in a function definition, some beginners try to write types in function calls too. Don't do that. For example,

  double z = largest(a, b, c);
makes sense. But
  double z = largest(double a, double b, double c);
is not allowed in C++. Remember that you are not defining function largest here, so get rid of the types in the function call.



Exercises

  1. What is wrong with the definition of horse in the following function definitions?

      int goat(int x, int y)
      {
        return x*y;
      }
    
      int horse(int x, int y)
      {
        return 2*goat(x)
      }
    
    Answer