What is function overloading ?

What is function overloading ?

ยท

1 min read

Function Overloading

  • When multiple functions declared in the same scope having same name but differ in arguments passed to it (number of arguments , type of arguments ) it is known as function overloading.
  • The key to function overloading is a function's argument list which is also known as signature. It is the signature not the function return type that enables function overloading.
  • Function overloading implements polymorphism.
  • It also reduces number of comparisons in a program and thereby makes the program runs faster.

Following code snippet is a example of function overloading.

void print(int i)                   //function 1
{
cout<<"Integer"<<" "<<i*i;
}

void print(char c)              //function 2
{
cout<<"character is"<<c;
}

void print(float f)              //function 3
{
cout<<"float"<<" "<<f*f;
}

//function calling 

print('c'); //as character is passed as a argument so function 2 will be called
print(1);  //as integer is passed as a argument so function 1 will be called
print(3.14); //as decimal is passed as argument so function 3 will be called
ย