Function Overloading in C++

Function in C++ is a group of program statements with a unique name that perform a specific task. Functions are used to provide modularity to a program.

function overloading in c++
Function Overloading in C++ is the mechanism by which a programmer can specify multiple definitions of the same function(same name) by changing:

  • Number of arguments passed to the function
  • Data type of arguments passed to the function

Essentially Function overloading means same function name but different number or type of arguments with different functionality.  Function overloading is also a type of Static or Compile time Polymorphism.

Overloading is a form of polymorphism. It allows the programmer to write functions to do conceptually the same thing on different types of data without changing the name. This allows consistency in notation, which is good both for reading and for writing code. This also allows the same method name to be reused across multiple implementations.

Example:

Notice that, the return type of all these 4 functions are not same. Overloaded functions may or may not have different return type but it should have different argument(s).

The number and type of arguments passed to these two functions are same even though the return type is different. Hence, the compiler will throw error.

Function Overloading in C++ Example Program

Run Online

Output
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
Watch it on YouTube

Leave a Reply

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