C++ Function Overloading

Function Overloading in C++ is a very handy tool that we can make use of while coding. It allows us to create several definitions for a function of the same name. You may also think of them as variants with different parameters. The variant that is picked, is decided at runtime, based on the parameters given in the function call.

The Obvious benefit of Function Overloading is that we can have different code run, depending on the parameters we use in the function call.

When it comes to OOP, Function Overloading is also an important part of Polymorphism, one of the three pillars of Object Oriented Programming in C++.


Function Signatures

Before we proceed, I should explain the Function signature concept. Each C++ function has a signature, which is used to identify it. The signature is constructed out of the function name, the number of parameters and the type of the parameters. The main idea here is that changing any of these things will create a different signature for that function.

And this is exactly what we want. By simply changing the number of parameters or the type of parameters, we can get a different function, even though it has the same name. This is because the signatures are different, hence they are unique. However, it is important for the names to be the same (for overloading), otherwise it’s just a completely different function.

Now, notice how the return type doesn’t have anything to do with the signature. This means that we cannot apply the function overloading concept to the return type. If you were to create a function of the same name with a different return type, the Compiler would generate the same signature. And of course, due to two same signatures, there would be an error.


Creating Overloaded Functions

Below is a short and simple example on Function overloading in C++. We have created two functions with the same name, but a different number of parameters. The first one will sum 2 numbers, and return the result. The second sums 3 numbers and returns the result.

int add(int num1, int num2) {
    return num1 + num2;
}

int add(int num1, int num2, int num3) {
    return num1 + num2 + num3;
}

The calling code for the above functions will look like this. You may also notice that there is no return type in the function call.

cout << add(5, 3) << endl;
cout << add(5, 2, 3) << endl;

At runtime, the compiler will automatically match the correct function calls to the correct signatures. This ensures that the right function is called.

Note: Avoid overloading for every little thing. Only do it for functions that actually do similar things. Often, you can lose information by keeping the names for several functions the same. (This is only a problem when overloading is done incorrectly).


Function Overloading with Parameter Type

Here we have another example of C++ Function Overloading, where we have the same name, same number of parameters but different parameter types. This is a good example for showing different behaviors, for functions of the same name.

void add(int num1, int num2) {
    cout << num1 + num2 << endl;
}

void add(string s1, string s2) {
    cout << s1 + s2 << endl;
}

int main() {
    add(5,2);
    add("2", "8");
}

The add() function with string parameters concatenates the two strings, whereas the one with integers, sums them up algebraically. The one that is called, depends on the input given at call time.

Output:

7
28

C++ Templates

If you are interested in the flexibility and ease of use provided by Function Overloading, chances are that you will be interested in Templates types as well. Templates types are the foundation of Generic Programming, which allow you to write type independent code.

If you use templates correctly, you can further shorten your code and make it more flexible.


This marks the end of our C++ Function Overloading Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments