Professional Documents
Culture Documents
Objectives
1. Writing overloaded functions.
2. Passing arguments by reference.
Function Overloading
With function overloading, multiple functions can have the same name with different
parameters:
Example:
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
Notice that all the functions have same name, but the only distinguishing factor is the
number of arguments being passed while calling the function. The different functions
with same names are also called prototypes of that function i.e. in above example
myFuntion have 3 prototypes.
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Output:
Exercise 8.1:
Write a C++ program using function overloading. The name of the overloaded
function is ‘square’. The different prototypes of this overloaded function are;
void square(void);// Used to print a solid square of *s with side length of 5
void square (char);// Used to print a solid square of the character passed as
argument with side length of 6
void square(char, int);// Used to print a solid square of the character and
the side length passed as arguments to this function
Make calls to these functions one by one in your program.
Web Resources
http://www.cplusplus.com/doc/tutorial/