Class 1 Class 2 Class 3 Class 4 Class 5 Class 6 Class 7 Class 8

Function Overloading in C++ for Beginners

Function Overloading in C++ is a feature that allows multiple functions to have the same name but different parameters. The functions can differ in the number, type, or order of arguments. Function overloading helps improve code readability and makes programs more flexible and easier to manage. It is an example of compile-time polymorphism in C++. By using function overloading, programmers can perform similar tasks using a single function name.

Function Overloading in C++

Function Overloading is one of the features of object-oriented languages. C++ provides 2 types of overloading:

  • Function overloading
  • Operator overloading

Function Overloading

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters (different number of arguments or different data types). The compiler decide which function will be call based on the parameters.

Advantages of Function Overloading

  • Improves code readability
  • Reduces function name complexity
  • Increases program flexibility
  • Supports different types of input with same function name

Question: Find the sum of two and three number using function overloading.

#include <iostream>
using namespace std;

// function to add two numbers
int add(int a, int b)
{
    return a + b;
}

// function to add three numbers
int add(int a, int b, int c)
{
    return a + b + c;
}

int main()
{
    cout << "Sum of 2 numbers = " << add(5, 10) << endl;
    cout << "Sum of 3 numbers = " << add(5, 10, 15);

    return 0;
}

Inline Function

An inline function is a function where compiler replaces the function call from the actual function code at the time of compilation. This helps to improve program execution speed and reduce the overhead of function calls. Inline functions are generally used for small functions.

Advantages of Inline Function

  • Faster execution
  • Reduces function call overhead
  • Useful for small functions

Syntax:

inline return_type function_name(parameters)
{
    // function body
}

Question: Write a program to find the square root.

#include <iostream>
using namespace std;

// inline function
inline int square(int x)
{
    return x * x;
}

int main()
{
    int num = 5;

    cout << "Square = " << square(num);

    return 0;
}

Recursion (Recursive function)

When function, calls itself within its body, it is called as recursive function. But note that the value of the argument of the passed argument nust be different. Recursion is also called as circular delinition. Note that all the functions can’t be made recursive.

Syntax:

return_type function_name(parameters)
{
    if(condition)
        return value;

    function_name(updated_parameters);  // recursive call
}

Question: Write a program to find the factorial using recursion.

#include <iostream>
using namespace std;

// recursive function
int factorial(int n)
{
    if(n == 0 || n == 1)
        return 1;

    return n * factorial(n - 1);
}

int main()
{
    int num = 5;

    cout << "Factorial = " << factorial(num);

    return 0;
}

Disclaimer: We have provide you with the accurate handout of “Functions in C++ for Beginners“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.

Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.

cbseskilleducation.com

Leave a Comment