Function Overloading in C++

Rishabh Agarwal
2 min readMar 14, 2023

Consider you are working on a maths library. Your current task is to build a set of functions that will add area calculation ability into your library. You plan to write functions for calculating area of circles, squares, and rectangles.

You went ahead and came up with the following header file.

double calculateAreaCircle(double radius);
int calculateAreaSquare(int sideLength);
int calculateAreaRectangle(int length, int breadth);

You’ve written a solution that works, but you’re not satisfied with the design. One of your major concerns is the function names, and rightly so — it’d be a nightmare for library users to remember all those different names. But fear not, a solution is at hand: after some research, you discover the perfect way to address all your concerns — Function Overloading.

What are overloaded functions?

Functions having the same name, similar functionality but different algorithms, and identified by different interfaces data types are called overloaded functions.

By leveraging the power of function overloading, you’re able to create a much more elegant and user-friendly interface for the area functions in your library. With function overloading, your code can handle a variety of different arguments in a seamless and intuitive way, saving users time and headaches down the line.

This is how the header file now looks.

double calculateArea(double radius);
int calculateArea(int sideLength);
int calculateArea(int length, int breadth);

Function overloading is also known as static polymorphism. It is called so because the binding between the actual function call and the function selection is based on the number and the types of the actual parameters at the place of actual invocation. This selection is performed by compiler.

Note: Two functions having the same signature but different return types can not be overloaded.

Default Parameters as Function Overloading

Default parameters in C++ are a special case of function overloading. Function overloading allows you to define multiple functions with the same name but different parameters. When you define a function with default parameters, you are actually defining multiple overloaded versions of the same function, each with a different set of parameters.

In conclusion, function overloading is an important and highly useful feature in C++, allowing developers to make their code more efficient, flexible, and maintainable. By mastering function overloading, programmers can write clean, concise, and highly optimized code that is easily understandable to themselves and their peers.

If you found this blog helpful, I encourage you to check out the other blogs in my “in C++” series and follow me on Twitter for more great coding tips and tricks.

If you enjoyed the content, please consider clapping and sharing it with your fellow developers. Thank you for reading! 🙂

--

--

Rishabh Agarwal

Software Engineer | Loves to write about Programming, Technology, and Mathematics!