Lesson 37 of 60 – Function Overloading in C++
62%

Function Overloading in C++

Function overloading allows us to create multiple functions with the same name but with different parameter lists. C++ decides which function to call based on the number, type, or order of the arguments.

Note: Function overloading makes programs easier to read because related operations can use the same meaningful function name.

1. What is Function Overloading?

Function overloading means defining multiple functions with the same name but different parameters.

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

Both functions are named add, but their parameter types are different.

2. Why Use Function Overloading?

Function overloading allows related operations to use one common function name.

  • Improves readability.
  • Reduces unnecessary function names.
  • Makes code easier to understand.
  • Allows functions to work with different data types.
  • Supports compile-time polymorphism.

3. Simple Function Overloading Example

#include <iostream>

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {

    std::cout << add(10, 20) << std::endl;

    std::cout << add(10.5, 20.5);

    return 0;
}

C++ selects the appropriate add() function based on the arguments.

4. Overloading by Number of Parameters

Functions can be overloaded by changing the number of parameters.

int add(int a, int b) {
    return a + b;
}

int add(int a, int b, int c) {
    return a + b + c;
}

The first function accepts two arguments, while the second accepts three.

5. Calling Overloaded Functions

int add(int a, int b) {
    return a + b;
}

int add(int a, int b, int c) {
    return a + b + c;
}

int main() {

    std::cout << add(10, 20) << std::endl;

    std::cout << add(10, 20, 30);

    return 0;
}

The number of arguments determines which overloaded function is called.

6. Overloading by Parameter Type

Functions can also be overloaded by changing the parameter data type.

int square(int number) {
    return number * number;
}

double square(double number) {
    return number * number;
}

One function accepts an int, while the other accepts a double.

7. Calling Based on Data Type

int square(int number) {
    return number * number;
}

double square(double number) {
    return number * number;
}

int main() {

    std::cout << square(5) << std::endl;

    std::cout << square(5.5);

    return 0;
}

The integer argument selects the integer version, while the decimal argument selects the double version.

8. Overloading with Different Parameter Types

void display(int number) {

    std::cout << "Integer: "
              << number;
}

void display(double number) {

    std::cout << "Double: "
              << number;
}

void display(char letter) {

    std::cout << "Character: "
              << letter;
}

All three functions have the same name but accept different data types.

9. Overloading by Parameter Order

The order of different parameter types can also be changed.

void display(int number, double value) {

    std::cout << number << " "
              << value;
}

void display(double value, int number) {

    std::cout << value << " "
              << number;
}

The parameter order makes the function signatures different.

10. Function Signature

For overloading, the parameter list is important. It includes the number, types, and order of parameters.

void show(int number) {
}

void show(double number) {
}

These functions have different parameter types, so they can be overloaded.

11. Return Type Alone Cannot Overload a Function

You cannot overload functions only by changing their return type.

The following is invalid:

int getValue() {
    return 10;
}

double getValue() {
    return 10.5;
}

Both functions have the same name and parameter list. Changing only the return type is not sufficient for overloading.

12. Two Parameters vs Three Parameters

int multiply(int a, int b) {

    return a * b;
}

int multiply(int a, int b, int c) {

    return a * b * c;
}

int main() {

    std::cout << multiply(2, 3) << std::endl;

    std::cout << multiply(2, 3, 4);

    return 0;
}

The correct function is selected according to the number of arguments.

13. Overloading with int and double

void print(int value) {

    std::cout << "Integer: "
              << value;
}

void print(double value) {

    std::cout << "Decimal: "
              << value;
}

int main() {

    print(100);

    std::cout << std::endl;

    print(25.5);

    return 0;
}

14. Overloading with Strings

#include <iostream>
#include <string>

void show(std::string name) {

    std::cout << "Name: "
              << name;
}

void show(int age) {

    std::cout << "Age: "
              << age;
}

int main() {

    show("Rahul");

    std::cout << std::endl;

    show(20);

    return 0;
}

The appropriate function is selected based on the argument type.

15. Overloading a Calculator Function

int calculate(int a, int b) {

    return a + b;
}

int calculate(int a, int b, int c) {

    return a + b + c;
}

int main() {

    std::cout << calculate(10, 20)
              << std::endl;

    std::cout << calculate(10, 20, 30);

    return 0;
}

Function overloading can make calculator-style programs easier to organize.

16. Overloading with Different Operations

int area(int side) {

    return side * side;
}

int area(int length, int width) {

    return length * width;
}

int main() {

    std::cout << "Square Area = "
              << area(5) << std::endl;

    std::cout << "Rectangle Area = "
              << area(5, 10);

    return 0;
}

The same function name can represent related operations with different parameter lists.

17. Overloading and Default Arguments

Care is needed when combining function overloading with default arguments because calls can become ambiguous.

void show(int a) {
    std::cout << a;
}

void show(int a, int b = 10) {
    std::cout << a << " " << b;
}

Calling show(5) can create ambiguity because both functions can accept one argument.

18. Overloading and Type Conversion

C++ can sometimes convert an argument to match an overloaded function. This can affect which function is selected.

void show(int value) {

    std::cout << "Integer";
}

void show(double value) {

    std::cout << "Double";
}

int main() {

    show(10);

    return 0;
}

The exact matching int version is selected for the integer argument.

19. Overloading with Three Different Versions

void display(int value) {

    std::cout << "Integer";
}

void display(double value) {

    std::cout << "Double";
}

void display(std::string value) {

    std::cout << "String";
}

A function name can have multiple overloaded versions as long as their parameter lists are different.

20. Compile-Time Polymorphism

Function overloading is an example of compile-time polymorphism. The compiler determines which overloaded function should be called during compilation.

void show(int value) {
    std::cout << "Integer";
}

void show(double value) {
    std::cout << "Double";
}

21. Overloading with Character Parameters

void print(char value) {

    std::cout << "Character: "
              << value;
}

void print(int value) {

    std::cout << "Integer: "
              << value;
}

int main() {

    print('A');

    std::cout << std::endl;

    print(65);

    return 0;
}

The character argument selects the character version, while the integer argument selects the integer version.

22. Practical Student Information Example

#include <iostream>
#include <string>

void student(std::string name) {

    std::cout << "Name: "
              << name;
}

void student(std::string name, int age) {

    std::cout << "Name: "
              << name << std::endl;

    std::cout << "Age: "
              << age;
}

int main() {

    student("Amit");

    std::cout << std::endl;

    student("Amit", 20);

    return 0;
}

The same function name provides different levels of student information.

23. Practical Maximum Number Example

int maximum(int a, int b) {

    return (a > b) ? a : b;
}

int maximum(int a, int b, int c) {

    int max = a;

    if (b > max) {
        max = b;
    }

    if (c > max) {
        max = c;
    }

    return max;
}

int main() {

    std::cout << maximum(10, 20)
              << std::endl;

    std::cout << maximum(10, 20, 30);

    return 0;
}

24. Practical Area Calculator

#include <iostream>

double area(double radius) {

    return 3.14159 * radius * radius;
}

double area(double length, double width) {

    return length * width;
}

int main() {

    std::cout << "Circle Area = "
              << area(5.0)
              << std::endl;

    std::cout << "Rectangle Area = "
              << area(5.0, 10.0);

    return 0;
}

25. Advantages of Function Overloading

  • Uses meaningful and consistent function names.
  • Improves code readability.
  • Reduces the need for different names for similar operations.
  • Allows functions to work with different data types.
  • Allows different numbers of parameters.
  • Supports compile-time polymorphism.
  • Can make reusable code easier to understand.

26. Common Mistakes in Function Overloading

  • Trying to overload only by changing the return type.
  • Creating two functions with exactly the same parameter list.
  • Using ambiguous default arguments.
  • Forgetting the parameter type difference.
  • Using unnecessary overloaded functions.
  • Assuming every numeric conversion will select the expected function.

27. Overloading vs Different Function Names

Without overloading, related operations might require different names:

int addTwo(int a, int b) {
    return a + b;
}

int addThree(int a, int b, int c) {
    return a + b + c;
}

With overloading, one meaningful name can be used:

int add(int a, int b) {
    return a + b;
}

int add(int a, int b, int c) {
    return a + b + c;
}

28. Best Practices

  • Use the same function name only for closely related operations.
  • Keep overloaded functions easy to distinguish.
  • Use meaningful parameter types.
  • Avoid unnecessary overloads.
  • Be careful with default parameters.
  • Avoid ambiguous function calls.
  • Do not use return type alone to create an overload.

29. Function Overloading Syntax

The general syntax for function overloading is:

returnType functionName(type parameter1) {
    // code
}

returnType functionName(type parameter1, type parameter2) {
    // code
}

The functions must have different parameter lists.

30. Function Overloading – Final Summary

Concept Meaning
Function Overloading Using the same function name with different parameter lists.
Parameter Count Functions can have different numbers of parameters.
Parameter Type Functions can use different parameter data types.
Parameter Order Different parameter orders can create different overloads.
Return Type Cannot be used alone to overload a function.
Compile-Time Polymorphism Function overloading is resolved by the compiler.
int add(int a, int b) {
    return a + b;
}

int add(int a, int b, int c) {
    return a + b + c;
}

📌 Key Points

  • Function overloading means using the same function name with different parameters.
  • Functions can be overloaded by the number of parameters.
  • Functions can be overloaded by parameter data types.
  • Functions can also differ by the order of parameter types.
  • Changing only the return type does not create a valid overload.
  • C++ selects the appropriate overloaded function based on the arguments.
  • Function overloading is an example of compile-time polymorphism.
  • Default arguments should be used carefully with overloaded functions.
  • Overloading is useful for related operations that work with different inputs.

🧠 Quick Quiz

Question: Which of the following is required for function overloading in C++?