C++C++11C++14C++17Introduction to C++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,

We can use this function as in this example below,

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

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,

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;

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

Worth reading...
Learn About Function Declaration and Definition in C++