How to Use Default Arguments in Function Overloading in C++? Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, we can provide the default values for the input arguments into the functions and it is also supported in function overloading. In this article, we will learn how to use default arguments in function overloading in C++. Default Arguments in Function Overloading in C++We can define the default value in function overloading as we do it in the main function. But the problem occurs in situations where there are multiple matching function definitions for a function call and the compiler cannot decide which function body to execute. For Example, see the function overloading of: int func(int a) { //body }int func(int a, int b = 10) { // body }Both of the above functions can be called using the call func(10) leading to ambiguity issues. C++ Program to Use Default Arguments in Function OverloadingThe below example demonstrates how we can use default arguments in function overloading in C++. C++ // C++ program to use default argument in function // overloading #include <iostream> using namespace std; // Function without default arguments void display(int a, int b, int c) { cout << "Values: " << a << " " << b << " " << c << endl; } // Overloaded function with default arguments void display(int a, int b = 10) { cout << "Values: " << a << " and " << b << endl; } int main() { display(5); // display(int a,int b=10) is called and // default value 10 is used here because // second arguement is missing display(5, 20); // display(int a,int b=10) is called but // two arguements are passed so default // value is not used here display(10, 15, 20); // overloaded function display(int // a,int b,int c) is called return 0; } OutputValues: 5 and 10 Values: 5 and 20 Values: 10 15 20 Note: When using default arguments with function overloading make sure that any call to an overloaded function is unambiguous and compiler must be able to select the correct function based on the function call. Comment More infoAdvertise with us Next Article Overloading function templates in C++ H harshsingh123 Follow Improve Article Tags : C++ Programs C++ C++-Function Overloading and Default Arguments cpp-overloading CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read Function Overloading vs Function Templates in C++ In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, 4 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read Overloading function templates in C++ Template: A template is a tool that reduces the efforts in writing the same code as templates can be used at those places.A template function can be overloaded either by a non-template function or using an ordinary function template. Function Overloading: In function overloading, the function may ha 3 min read How to Create Custom Assignment Operator in C++? In C++, operator overloading allows us to redefine the behavior of an operator for a class. In this article, we will learn how to create a custom assignment operator for a class in C++. Creating Custom Assignment (=) Operator in C++To create a custom assignment operator, we need to define an overloa 2 min read Like