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!

close

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

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