Function Overloading: Different Parameters and Arguments

In this tutorial from our C++ e-book, we will learn what function overloading is, what it is for, and how to use this important programming technique.

Parameter and Arguments Sizes in C++ Functions

In the Default Arguments tutorial, we saw that you can send a variety of arguments to a function as long as it is using reference parameters.

For example, the code below is from a function that calculates an arithmetic average:
#include <iostream>
using namespace std;

float average(float a, float b, float c, float d = 0.0)
{
    return (a+b+c+d)/4;
}

int main()
{
    cout<<"Average of 3 numbers: "<<average(10, 9, 7)<<endl;
    cout<<"Average of 4 numbers: "<<average(10, 9, 7, 6)<<endl;

    return 0;
}
It can take 3 or 4 arguments. If you submit only 3, the 4 argument is the default, with a value of 0.
However, note an error.

Even if we only send 3 arguments, the arithmetic average is calculated as if there were 4 notes.

It would be interesting if, if I submitted 3 arguments, it would return:
(a + b + c) / 3

And if I sent 4 arguments, it would return:
(a + b + c + d) / 4

And this is possible with overloading functions.

C++ Function Overloading

Overloading is the technique that allows us to have functions with the same name as long as their parameters are different, in some way.

Here's what the 3 and for grade average program looks like:
#include <iostream>
using namespace std;

float average(float a, float b, float c)
{
    return (a+b+c)/3;
}

float average(float a, float b, float c, float d)
{
    return (a+b+c+d)/4;
}

int main()
{
    cout<<"Average of 3 numbers: "<<average(10, 9, 7)<<endl;
    cout<<"Average of 4 numbers: "<<average(10, 9, 7, 6)<<endl;

    return 0;
}
Note that we invoke the average() function, the only difference is that in the first call we pass 3 arguments, and in the second call of the function we pass 4 arguments.

Because C++ is naughty and smart, it knows which function to run correctly.

It is so clever that even if there are the same number of parameters / arguments, it can differentiate through the type of data we are using.

Look:
#include <iostream>
using namespace std;

double average(double a, double b, double c)
{
    cout<<"Average of 3 doubles   : ";
    return (a+b+c)/3;
}

int average(int a, int b, int c)
{
    cout<<"Average of 3 integers  : ";
    return (a+b+c)/3;
}

int main()
{
    cout<<average(10.0, 9.0, 7.0)<<endl;
    cout<<average(10, 9, 7)<<endl;

    return 0;
}
When we pass double type variables, it calls average(double, double, double)
When we pass int variables, it calls average(int, int, int)

C++ can differentiate because the signatures of each function are different (either in the number of parameters or the type that will work).

In fact, even if the functions have the same name, the same number of parameters, and the same data types, we can overload functions as long as the order of the parameters is different:
#include <iostream>
using namespace std;

void func(int a, double b)
{
    cout<<"First is int    : "<<a<<endl;
    cout<<"Second is double: "<<b<<endl;
}

void func(double b, int a)
{
    cout<<"First is double : "<<b<<endl;
    cout<<"Second is int   : "<<a<<endl;
}

int main()
{
    func(1, 2.5);
    cout<<endl;
    func(2.5, 1);

    return 0;
}
In the example we have: func (int, double)
Also: func (double, int)

Simply signature from one function to another is different so we can use overload where signature is a set: function name, data type, data number and order of information.

For good programming practices, you should use function overloading whenever you need to use functions with the same purpose and logic, but for different number and / or types and / or order of data.

No comments:

Post a Comment