Function Overloading in C++

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.

Function overloading can be considered as an example of polymorphism feature in C++.

Following is a simple C++ example to demonstrate function overloading.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
void print(int i) {
  cout << " Here is int " << i << endl;
}
void print(double  f) {
  cout << " Here is float " << f << endl;
}
void print(char const *c) {
  cout << " Here is char* " << c << endl;
}
  
int main() {
  print(10);
  print(10.10);
  print("ten");
  return 0;
}

chevron_right


Output:

Here is int 10 
Here is float 10.1 
Here is char* ten 
  1. Function overloading and return type
  2. Functions that cannot be overloaded in C++
  3. Function overloading and const keyword
  4. Function Overloading vs Function Overriding in C++

Recent articles on function overloading in C++

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : raHUL GUPTA 23