C++ Function Overloading

This tutorial explains the concept of C++ function overloading and how it is used in programs.

C++ programming function overloading

As we know that functions are the piece of code that can be used anywhere in the program with just calling it multiple times to reduce the complexity of the code. In POP, we can use as many functions as per need, however, the names of the function shouldn’t match. In the case of OOP, we can use a function name as many times following the condition that the number of arguments or the type of arguments must differ.

So the method of using the same function name for different functions is simply called function overloading. When an overloaded function is called from main program the function with matching arguments is invoked.

Explanation
In above program, when we call the function with no arguments the display()function with no arguments is invoked and the result is 3. Similarly, when we pass one integer type argument while calling the display(int a) function is invoked. Similarly, when we pass integer and float value display(int a, float b) function is invoked. The advantage of function overloading is that the number of names of functions is reduced.

So function overloading is achieved by varying following points:

the number of parameters

datatype of the parameters

the order of the appearance of parameters

Common Programming Error

While defining overloaded function with identical parameter lists but different return type results in a compilation error.

Related Articles:

C++ Constructor Overloading Likewise function overloading, a class can have more than one constructor. Since more than one constructor is defined in a...

Python float() Function The Python float() is a built-in function that returns a floating point number constructed from a number or a string....

C++ Constructors In this tutorial, you will learn in depth about C++ constructors and its types with examples. C++ Constructors: Introduction Types...

C programming function arguments C programming function arguments also known as parameters are the variables that will receive the data sent by the calling program....