Function Overloading

 

This property is special to C++. Using Function overloading multiple functions can have the same name with different parameters.

 

Example:

 

#include <iostream>

using namespace std;

int addFunc(int a, int b) {
  return a + b;
}

double addFunc(double a, double b) {
  return a+b ;
}

int main() 
{
  int num1 = addFunc(3, 5);
  double num2 = addFunc(2.1, 6.2);
  cout << "Int: " << num1 << "\n";
  cout << "Double: " << num2;
  return 0;
}

 

 

Output:

Int : 8
Double : 8.3

 

Note: It is better to over load one function instead of defining two functions to do the same thing. Remember multiple functions can have the same name until their datatype or parameters differ.