My Bookmarks

  • No bookmark found

Most Active Members

Function Overloading in C/Cpp1 min read

Function overloading is a c++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters.

For example the parameters list of a function myfun(int a, float b) is (int, float) which is different from the function myfun(float a, int b) parameter list (float, int).

Function overloading is a compile-time polymorphism.

Example:

#include<iostream.h>
void display(int a);
void display(float b);
void display(int a, float b);
void main()
{ int a= 5;
 float b=5.5;
 display(a);
 display(b);
 display(a,b);
}
void display(int a)
{
 cout<<”Integer number:”<<a;
}  
void display(int b)
{
 cout<<”Float number:”<<b;
}  
void display(int a, int b)
{
 cout<<”Integer number:”<<a<<”and float number:”<<b;
} 

Output

Integer number:5
Float number:5.5
Integer number:5 and float number:5.5

Advantage of function overloading

  • It speeds up the execution of programs.
  • The use of function overloading is to save the memory space, consistency, and readability.
  • Function overloading exhibits the behavior of polymorphism which helps to get different behavior, although there will be some link using same name of function.
  • Function overloading is done for code re-usability, to save efforts, and also to save memory.
  • Code Maintenance is easy.

Disadvantage of function overloading

  • Function declarations that differ only in the return type cannot be overloaded.
  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is static member function declaration.
Bookmark(0)
Tags:

Add a Comment

Your email address will not be published. Required fields are marked *