Overloaded Functions

Recall the function cube():
int cube(int number)
{
  int value;

  value = number * number * number;
  return value;
}
Example calls:
int i;
float f;
i = cube(2);     // i is 8
f = cube(2.5f);  // f is 8.0
We need another function so we can cube floats:
float floatCube(float number)
{
  float value;

  value = number * number * number;
  return value;
}
Example calls:
i = cube(2);          // i is 8
f = floatCube(2.5f);  // f is 15.625f
This can quickly become unmanageable as we write functions to handle other types: doubleCube(), longCube(), charCube() ...

Next page

Back to Index
Back to Outline