codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

21 / 87 topics
18Function Basics & User-defined Types19Function Parameters20Inline Functions21Function Overloading22Scope23Recursion24Lambda Expressions
Tutorials/C++ Programming/Function Overloading
⚡C++ Programming

Function Overloading

Updated 2026-05-12
30 min read

Function Overloading

Function overloading is a powerful feature in C++ that allows you to define multiple functions with the same name but different parameters. This feature enhances code readability and reusability by allowing functions to perform similar tasks with different sets of inputs. In this tutorial, we'll explore how to overload functions by changing their parameter count, types, and names, understand the concept of name mangling, and learn about common pitfalls to avoid.

Introduction

Function overloading is a technique in C++ where multiple functions can have the same name but differ in their parameters (such as number or type). This allows you to use the same function name for different operations, making your code more intuitive and easier to manage. For example, you might have an add function that adds two integers and another add function that adds two floating-point numbers.

Function overloading is particularly useful in scenarios where functions perform similar tasks but with different data types or input requirements. By using function overloading, you can write cleaner and more maintainable code.

Overloading by Parameter Count

One way to overload functions is by changing the number of parameters they accept. This allows a single function name to handle different numbers of inputs.

Example 1: Adding Two Integers

C++
1#include <iostream>
2
3int add(int a, int b) {
4 return a + b;
5}
6
7int main() {
8 std::cout << "Sum of two integers: " << add(3, 4) << std::endl;
9 return 0;
10}
Output

Explanation

In the above examples, we have two functions named add, one that takes two parameters and another that takes three. The compiler can distinguish between these functions based on the number of arguments passed to them.

Overloading by Parameter Type

Another way to overload functions is by changing their parameter types. This allows a single function name to handle different data types.

Example 3: Adding Two Integers and One Float

C++
1#include <iostream>
2
3int add(int a, int b) {
4 return a + b;
5}
6
7float add(float a, float b) {
8 return a + b;
9}
10
11int main() {
12 std::cout << "Sum of two integers: " << add(3, 4) << std::endl;
13 std::cout << "Sum of two floats: " << add(3.5f, 4.2f) << std::endl;
14 return 0;
15}
Output
Sum of two integers: 7
Sum of two floats: 7.7

Explanation

In this example, we have two add functions: one that takes two integers and another that takes two floats. The compiler can differentiate between these functions based on the types of arguments passed to them.

Overloading by Parameter Order

While less common, you can also overload functions by changing the order of their parameters. However, this is generally not recommended as it can lead to confusion.

Example 4: Adding Two Integers with Different Orders

C++
1#include <iostream>
2
3int add(int a, int b) {
4 return a + b;
5}
6
7int add(int b, int a) { // Function signature is the same, so this will cause an error
8 return a + b;
9}
10
11int main() {
12 std::cout << "Sum of two integers: " << add(3, 4) << std::endl;
13 return 0;
14}

Explanation

In this example, attempting to overload the add function by changing the order of its parameters results in a compilation error because the function signatures are identical.

Warning

Function overloading based on parameter order is not recommended as it can lead to confusion and make your code harder to understand.

Name Mangling

When you compile C++ code, the compiler generates unique names for functions based on their signatures. This process is known as name mangling. It allows the linker to distinguish between different overloaded functions.

Example 5: Understanding Name Mangling

C++
1#include <iostream>
2
3int add(int a, int b) {
4 return a + b;
5}
6
7float add(float a, float b) {
8 return a + b;
9}
10
11int main() {
12 std::cout << "Sum of two integers: " << add(3, 4) << std::endl;
13 std::cout << "Sum of two floats: " << add(3.5f, 4.2f) << std::endl;
14 return 0;
15}

Explanation

In the above example, both add functions have unique names generated by the compiler based on their parameter types. This allows the linker to resolve calls correctly.

Ambiguity Errors

Ambiguity errors occur when the compiler cannot determine which overloaded function to call based on the provided arguments. This can happen if multiple functions match the given parameters equally well.

Example 6: Causing an Ambiguity Error

C++
1#include <iostream>
2
3int add(int a, int b) {
4 return a + b;
5}
6
7float add(float a, float b) {
8 return a + b;
9}
10
11int main() {
12 std::cout << "Sum of two integers: " << add(3, 4.5f) << std::endl; // Ambiguity error
13 return 0;
14}

Explanation

In this example, the add function call with arguments 3 and 4.5f is ambiguous because both int add(int a, int b) and float add(float a, float b) can accept these types of arguments. The compiler cannot determine which function to call.

Caution

Ambiguity errors occur when the compiler cannot resolve which overloaded function to call. Ensure that your function signatures are distinct enough to avoid such errors.

Practical Example

Let's create a practical example where we overload functions to handle different types of data for calculating the area of shapes.

C++
1#include <iostream>
2#include <cmath>
3
4// Function to calculate the area of a rectangle
5double area(double length, double width) {
6 return length * width;
7}
8
9// Function to calculate the area of a circle
10double area(double radius) {
11 return M_PI * radius * radius;
12}
13
14int main() {
15 std::cout << "Area of rectangle (length=5, width=3): " << area(5.0, 3.0) << std::endl;
16 std::cout << "Area of circle (radius=4): " << area(4.0) << std::endl;
17 return 0;
18}
Output
Area of rectangle (length=5, width=3): 15
Area of circle (radius=4): 50.2655

Explanation

In this example, we have overloaded the area function to calculate the area of a rectangle and a circle. The compiler can distinguish between these functions based on the number of parameters.

Summary

  • Function overloading allows you to define multiple functions with the same name but different parameters.
  • You can overload functions by changing their parameter count, types, or names.
  • Name mangling is the process where the compiler generates unique names for overloaded functions.
  • Ambiguity errors occur when the compiler cannot determine which overloaded function to call.
Key ConceptsDescription
Function OverloadingDefining multiple functions with the same name but different parameters.
Name ManglingCompiler-generated unique names for overloaded functions.
Ambiguity ErrorsCompilation errors caused by ambiguous function calls.

What's Next?

In the next tutorial, we will explore Scope in C++. Scope determines where variables and functions are accessible within your code. Understanding scope is crucial for managing variable lifetimes and avoiding naming conflicts. Stay tuned!


PreviousInline FunctionsNext Scope

Recommended Gear

Inline FunctionsScope