Function Overloading


Many functions can have the same name but they should have different number of arguments or the types of the arguments should be different. This is known as function overloading.

Consider the example given below :

// A PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING

void display( );                                       // Function declaration – This function has no arguments
void display (char);                               // One argument
void display (char, int);                          // Two arguments
void main( )
{
display ( );
display (‘=’);
display (‘+’, 30);
}
void display( )
{
cout<< “Hi!”;
}
void display (char ch)
{
cout <<ch;
}
void display (char ch, int n)
{
int i;
for ( i = 0, i < n, i ++ )
cout<<ch;
}

In the above program, at the starting itself, the compiler can see that there are three functions with the name of display. But the compiler is also clever enough to see the arguments of each of the three functions. Once it sees that the arguments are different, the compiler will consider the three functions to be different.


In the next section we will take a look at : Recursion

Or you can go back to : Contents


Copyright © 2003 Sethu Subramanian All rights reserved.