What is function overloading??

What is function overloading?

- Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any situation.
- Consider following program which overloads MyFunction() by using different types of parameters :
#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
   cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
   cout <<MyFunction(5.4)<<”\n”; //calls MyFunction(double d);
   return 0;
}
int MyFunction(int i)
{
   return i*i;
}
double MyFunction(double d)
{
   return d*d;
}
- Now the following program overloads MyFunction using different no of parameters :
#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
   cout <<MyFunction(10)<<”\n”; //calls MyFunction(int i);
   cout <<MyFunction(1, 2)<<”\n”; //calls MyFunction(int i, int j);
return 0;
}
int MyFunction(int i)
{
   return i;
}
double MyFunction(int i, int j)
{
   return i*j;
}
- Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.
What are default arguments in functions? - C++
What are default arguments in functions? - C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function....
What is static function? - C++
What is static function? - Static member functions are used to maintain a single copy of a class member function across various objects of the class....
What does extern mean in a function declaration?
What does extern mean in a function declaration? - An extern function or a member can be accessed outside the scope of the .cpp file in which it was defined....
Post your comment