Function-Overloading in CPlusPlus

Function-Overloading in CPlusPlus

When two or more than two functions are declared in a program which are having the same name but different arguments/ parameters; we say function-overloading has occurred.

In function-overloading the difference of the functions can be in the number of arguments of in the types of arguments.

A function is a piece of code that is use to accomplish a specific task. The very common syntax for a function is as below;

In case the name of more than one function in a program is the same. While the number of parameters in the functions are different or their type is different, we say function-overloading has happened. The return type of the function can be the same or different.

For Example; the following functions are overloaded if they are declared all in the same program.

Note: It is important to remember that a function cannot be overloaded by its return type. For function-overloading parameters should be different in type or number.

The following code will produce an error.

Why? Because the return types of the functions cannot be counted as function-overloading.

See Also:   Functions in CPlusPlus

Below you will find a complete program that has demonstrated the concept of function-overloading with the function volume ();

Program:

In the above example; the function volume () is overloaded. The first function accepts one parameter, the second one accepts two parameters and the third one accepts three parameters. While the number of parameters/ arguments are different in the functions above and the name of the functions are the same; we say function-overloading has occurred.

Output:

Function-OverLoading Output

Please feel free to put your comments below and suggest us what other topics would you like to cover in the upcoming articles.

Previous CPP Tutorials: Classes in C++ and OOP Concept
Next How to Work With Libraries in C++

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like