Default Arguments in C++
A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.This article is explaining the working of the Default Arguments in C++ with programming example.
In this article, we will discuss default arguments in C++. To understand this, you must have a clear understanding of C++ functions.
When we define a function, we pass specific values to it, called arguments. By default, function arguments in C++ are passed βby value.β They are also known as parameters.
Now, let us understand the concept of default arguments in detail. We will be covering the following sections:
- What are Default Arguments?
- Working of Default Arguments
- Example of Default Arguments in C++
- Points to Remember about Default Arguments
- Endnotes
What are Default Arguments?
A C++ default argument is a value in the C++ function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.
However, if arguments are passed when the function is called, the default arguments are ignored.
Rules for defining default arguments β
- The values passed in the default arguments can be overwritten if a value is explicitly passed to the function.
- During the function call, the values are copied starting from left to right.
Preparing for a C++ interview? Check out the Top C++ Interview Questions and Answers
Also Read β Understanding Operators in C++
Learn more: Basics of C Programming Language
Related Read β Binary Search in C++
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Working of Default Arguments
Letβs look at the following cases of a code snippet:
Case 1:
Here, when the function temp() is called, both the default arguments are used by the function.
Case 2:
Here, when the function temp(4) is called, the first argument becomes 6, while the second argument still uses the default value.
Case 3:
Here, when the function temp(4, -3.5) is called, both the default arguments are overridden by the values explicitly passed to the function inside main().
Case 4:
In this case, when the function temp(3.5) is called, the function throws an error because we are trying a pass a floating point number, which should be the second argument, and it cannot be passed without passing the first argument.
Example of Default Arguments in C++
Example:
#include <iostream>using namespace std;
//define the default argumentsvoid show(char = '$', int = 5);
int main() { int count = 7;
cout << "No argument is passed: "; //Default arguments will be displayed show(); cout << "First argument is passed: "; //only the second argument will be default show('#'); cout << "Both arguments are passed: "; show('*', count);
return 0;}
void show(char c, int n) { for(int i = 1; i <= n; ++i) { cout << c; } cout << endl;}
Output:
What have we done here?
We first create a function called show() that has no arguments. In this case, the function show() uses the default arguments c = β$β and n = 5.
Next, the function show(β#β) is called with one argument. So, the first default argument is overwritten with the value c = β#,β and the second default argument is retained, i.e., n = 5.
Now, the function show(β*,β count) is called with both arguments. So, both the default arguments are overwritten with the values c = β*β and n = 7. In this case, the default arguments are not used at all.
Points to Remember about Default Arguments
- The default argument must be implicitly convertible to the parameter type.
- Once a default value for an argument is given, all subsequent arguments must also have default values.
- If the default arguments are defined in the function definition instead of the function prototype, the function must then be defined before the function call.
Endnotes
I hope this article was helpful for you in understanding default arguments in C++. If you want to learn more about C++ and solidify your basics, you can explore our articles on C++.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio