Function Overloading in C++

One significant addition made to the capabilities of function in C++ is that of function overloading. With this facility you can have multiple functions with the same name, unlike C, where all the functions in a program must have unique names.

In C, every function has to have a unique name. At times this becomes annoying. For example, there are following three different functions that return the absolute value of an argument:

All these functions do the same thing, so it seems unnecessary to have three different function names. C++ overcomes this situation by allowing the programmer to create three different functions with the same name. This is called function overloading.

Let us illustrate function overloading with an example as follows:

Here, two different functions, all named “max” are defined. The compiler checks their parameter lists to determine which one to use on each call. For example, the first call [max(99, 77)] passes two int’s, so the version that has two int’s in its parameter list is being called.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *