Site icon Learn C++

Discover Function Overloading in C++

Do you want to learn how to overload a function ? What is Function Overloading ? What does overloaded function means ? How we can use Function Overloading in C++ ? How we can use multiple parameter types in our functions ? Here we go,

Function Overloading is used to operate with the same function by defining it with different variable types. By using Function Overloading, a function can be used with the same name in different parameter types and multiple variations.

In previous posts about functions, we created an addition function add() that has a and b parameters and returns sum of a and b. C++ functions are the most flexible and effective functions for using memory and CPU operations. For example, using integer variables in a function is faster than using long integer variables. Also using correct types may have some benefits to use variables (i.e memory usage, conversion required, etc.). In programming sometimes we may want to use different types of variables and variable variations, here we use Function Overloading.

Let’s remember this integer addition example,

[crayon-66224c2e00666546821713/]

We can use this function as in this example below,

[crayon-66224c2e0066e056832572/]

As same this function we can generate a new function to add floating numbers with a name change,

[crayon-66224c2e00670984191679/]

As you see we should use a new name addf() for the operation on the floating point numbers.

If we want to use same function name on both float and integer numbers, we can Overload this function by adding a new declarations and definitions with the same function names as below,

[crayon-66224c2e00673716985573/]

As you see we have same function name but both has different parameter types and return types while both are used to add a and b;

[crayon-66224c2e00675783994697/]

Get started building powerful apps with C++Builder!

Exit mobile version