There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example:
void foo(int a, int b, int c = -1) {
std::cout << "foo(" << a << ", " << b << ", " << c << ")\n";
}
int main() {
foo(1, 2); // output: foo(1, 2, -1)
// void foo(int a, int b = 0, int c);
// error: does not use default from surrounding scope
void foo(int a, int b, int c = 30);
foo(1, 2); // output: foo(1, 2, 30)
// void foo(int a, int b, int c = 35);
// error: we cannot redefine the argument in the same scope
// has a default argument for c from a previous declaration
void foo(int a, int b = 20, int c);
foo(1); // output: foo(1, 20, 30)
void foo(int a = 10, int b, int c);
foo(); // output: foo(10, 20, 30)
{
// in inner scopes we can completely redefine them
void foo(int a, int b = 4, int c = 8);
foo(2); // output: foo(2, 4, 8)
}
return 0;
}
Online version to play with: http://ideone.com/vdfs3t
These possibilities are regulated by the standard in 8.3.6. More specific details are in 8.3.6/4
For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter pack. A default argument shall not be redefined by a later declaration (not even to the same value) ...
To tell the truth I never use this feature when coding in c++. I used similar code snippets several times to surprise my colleagues, but certainly not in the production code. Thus, the question is: Do you know real world examples of the code that use these features with benefits?
Default arguments are overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.
D. Default arguments are useful in situations where some arguments always have the same value.
The default arguments are used when you provide no arguments or only few arguments while calling a function. The default arguments are used during compilation of program.
In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.
If you cannot change some existing code or library and you really cannot be bothered to type the correct argument then changing the default argument for some scope could be a solution.
It seems like the kind of hack that could be useful when working with C++ code generated by some legacy tool. For example, if the generated code has always had hundreds calls into some external library using a default argument but now the default argument is no longer correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With