Function overloading

Aug 31, 2018

As we prefer naming the function name that is related to its functionality, there can be more than one functions that can have same name. In C Programming naming the function name with same name for more than one functions is not supported; C++ on the other hand supports such mechanism.

Function overloading is the programming concept that allows the programmers to define more than one functions with the same name and in the same scope; but functions has unique signatures, that means the mechanism to differentiate the functions. Let's see the simple example to return the square of the number using function overloading for int, float and double type.

int square(int num);

float square(float num);

double square(double num);

In above example, the function name are same, and what makes the functions to differentiate from others is the type of arguments, during function call the function that is called is detected by the compiler at compile time based on the type of arguments passed. i.e. if function is called as square(3); the square() function related to int is called.

The function overloading can be achieved by the following ways, or there are following types of function overloadig.

  1. Type of Arguments
  2. Number of Arguments
  3. Type and Number of Arguments

 

1)Type of Arguments

    This type of overloading is best matched for the same operation or functions to be performed for the different types of data. Function is overloaded based on the type of arguments. Let's consider the example for the squaring the number for different types of data.

#include<iostream>
using namespace std;

// function declarations or headers
int square(int num);
float square(float num);
double square(double num);

int main(){
	int x = 10;
	float y = 11.22;
	double z = 22.33;
	cout<<"Square of "<<x<<" = "<<square(x)<<endl;
	cout<<"Square of "<<y<<" = "<<square(y)<<endl;
	cout<<"Square of "<<z<<" = "<<square(z)<<endl;
	return 0;
}
// function definations
int square(int num){
	return num * num;
}
float square(float num){
	return num * num;
}
double square(double num){
	return num * num;
}

Sample Run:

Square of 10 = 100
Square of 11.22 = 125.888
Square of 22.33 = 498.629

 

2) Number of Arguments

In this type of overloading, compiler differentiates the function based on the number of arguments. Let's take an example to return the sum of numbers.

#include<iostream>
using namespace std;

// function declarations or headers
int sum(int n1, int n2);
int sum(int n1, int n2, int n3);
int sum(int n1, int n2, int n3, int n4);

int main(){
	int x = 10, y = 15, z = 20, a = 25;
	cout<<"Sum of "<<x<<" and "<<y<<" is "<<sum(x,y)<<endl;
	cout<<"Sum of "<<x<<" and "<<y<<" and "<<z<<" is "<<sum(x,y,z)<<endl;
	cout<<"Sum of "<<x<<" and "<<y<<" and "<<z<<" and "<<a<<" is "<<sum(x,y,z,a)<<endl;
	return 0;
}
// function definations
int sum(int n1, int n2){
	return n1 + n2;
}
int sum(int n1, int n2, int n3){
	return n1 + n2 + n3;
}
int sum(int n1, int n2, int n3, int n4){
	return n1 + n2 + n3 + n4;
}

Sample Run:

Sum of 10 and 15 is 25
Sum of 10 and 15 and 20 is 45
Sum of 10 and 15 and 20 and 25 is 70

 

3)Type and Number of arguments

The function is also overloaded by mixing above both types. That means compiler differentiates the function based on type and number of arguments passed. Let's see the following example.

#include<iostream>
using namespace std;

// function prototype
void display(int  num);
void display(int num, char ch);
void display(float num, int num1, char ch);

int main(){
	display(3);
	display(3, 'A');
	display(3.3, 5, 'K');
	return 0;
}
// function definations
void display(int num){
	cout<<"This function has integer "<<num<<endl;
}
void display(int num, char ch){
	cout<<"This function has integer "<<num<<" and character "<<ch<<endl;
}
void display(float num, int num1, char ch){
	cout<<"This function has float "<<num<<" integer "<<num1<<" and character "<<ch<<endl;
}

Sample Run:

This function has integer 3
This function has integer 3 and character A
This function has float 3.3 integer 5 and character K

 

 

Related


Tools/Apps





© Nepal Exchange Rates