The advantage of using the default function parameter

int add (int x, int y=1) int main () { int result1 = add(5); int result2 = add(5, 3); result 0; } 

VS

 int add (int x, int y) int main () { int result1 = add(5, 1); int result2 = add(5, 3); result 0; } 

What is the advantage of using the default function parameter in terms of speed, memory usage, etc.? For beginners like me, I was sometimes confused before I realized this was using the default function parameter; Is it not coding without a function parameter by default, which makes reading codes easier?

+3
source share
7 answers

Your add function is not a good example of how to use the default parameters, and you are right that with one it is more difficult to read.

However, this is not true for all functions. Consider std :: vector :: resize, which looks something like this:

 template<class T> struct vector_imitation { void resize(int new_size, T new_values=T()); }; 

Here resizing without providing a value uses T (). This is a very common case, and I believe that almost everyone finds a one-parameter call for resizing in order to understand:

 vector_imitation<int> v; // [] (v is empty) v.resize(3); // [0, 0, 0] (since int() == 0) v.resize(5, 42); // [0, 0, 0, 42, 42] 

The new_value parameter is created even if it is never needed: when resizing to a smaller size. Thus, for some overload functions, it is better than the default parameters. (I would include vector :: resize in this category.) For example, std :: getline works this way, although it has no other choice, since the default value for the third parameter is calculated from the first parameter. Something like:

 template<class Stream, class String, class Delim> Stream& getline_imitation(Stream &in, String &out, Delim delim); template<class Stream, class String> Stream& getline_imitation(Stream &in, String &out) { return getline_imitation(in, out, in.widen('\n')); } 

Default parameters would be more useful if you could specify named parameters for functions, but C ++ does not make this easy. If you encounter default parameters in other languages, you need to keep this restriction in C ++. For example, imagine a function:

 void f(int a=1, int b=2); 

You can only use the default setpoint for a parameter if you also use the default setpoints for all later parameters instead of ringing, for example:

 f(b=42) // hypothetical equivalent to f(a=1, b=42), but not valid C++ 
+5
source

If there is a default value that will ensure the correct behavior for a large amount of time, then it saves your code that constantly passes the same value. It just makes things simpler than writing foo (SOME_DEFAULT) everywhere.

+4
source

It has many uses. I usually use them in class constructors:

 class Container { // ... public: Container(const unsigned int InitialSize = 0) { // ... } }; 

This allows the class user to do the following:

 Container MyContainer; // For clarity. 

And this:

 Container MyContainer(10); // For functionality. 
+2
source

Like everything else, it depends.

You can use it to make the code more understandable.

 void doSomething(int timeout=10) { // do some task with a timeout, if not specified use a reasonable default } 

Better than having lots of doSomething (10) magic values ​​throughout your code

But be careful when you really need to do function overloading.

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

As Ed Swangren noted, some functions have parameters that tend to have the same meaning in most calls. In these cases, this value can be specified as the default value. It will also help you see the “suggested” value for this parameter.

Another case where this is useful is refraction, when you add some functions and a parameter for it to the function, and you don't want to break the old code. For example, strlen(const char* s) computes the distance to the first character \0 in a string. You may need to find another characteristic so that you can write a more general version: strlen(const char* s, char c='\0') . This will reuse the old strlen code without breaking compatibility with the old code.

The main problem with the default values ​​is that when viewing or using code written by others, you may not notice this hidden parameter, so you will not know that the function is more powerful than you can see from the code.

In addition, google's coding style suggests avoiding them.

+2
source

The default parameter is a function parameter that has a default value provided to it. If the user does not specify a value for this parameter, the default value will be used. If the user sets a value for the default parameter, the user value is used. In computer programming, the default argument is an argument to a function that the programmer should not specify. In most programming languages, functions can take one or more arguments. Usually, each argument must be specified completely (this is the case in the C programming language)

+1
source

The benefits of using the default parameter, as others have pointed out, are indeed the "clarity" that it introduces into the code with respect to function overloading.

But it’s important to keep in mind the main drawback of using this language compilation time function: binary compatibility and the default function parameter do not go hand in hand. For this reason, it is always useful to avoid using the default parameters in the API / interface classes. Because every time you change the default setting to something else, your clients must be recompiled as well as reconnected.

Symbian has very good C ++ design patterns to avoid such a BC.

0
source

All Articles