Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value of function parameter

1.

int Add (int a, int b = 3); int Add (int a, int b) {  } 

2.

int Add (int a, int b); int Add (int a, int b = 3) {  } 

Both work; which is the standard way and why?

like image 806
httpinterpret Avatar asked May 16 '10 07:05

httpinterpret


People also ask

Which parameters are default values?

A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.

What are default values in a function?

A function default value is a value that can be used for a parameter if the calling statement does not pass an argument. If an argument is provided, the default value is ignored.

What is default parameter value in C++?

The default constructor with argument has a default parameter x, which has been assigned a value of 0.

What is a default parameter value python?

Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.


2 Answers

If you put the declaration in a header file, and the definition in a separate .cpp file, and #include the header from a different .cpp file, you will be able to see the difference.

Specifically, suppose:

lib.h

int Add(int a, int b); 

lib.cpp

int Add(int a, int b = 3) {    ... } 

test.cpp

#include "lib.h"  int main() {     Add(4); } 

The compilation of test.cpp will not see the default parameter declaration, and will fail with an error.

For this reason, the default parameter definition is usually specified in the function declaration:

lib.h

int Add(int a, int b = 3); 
like image 161
Greg Hewgill Avatar answered Oct 01 '22 13:10

Greg Hewgill


In C++ the requirements imposed on default arguments with regard to their location in parameter list are as follows:

  1. Default argument for a given parameter has to be specified no more than once. Specifying it more than once (even with the same default value) is illegal.

  2. Parameters with default arguments have to form a contiguous group at the end of the parameter list.

Now, keeping that in mind, in C++ you are allowed to "grow" the set of parameters that have default arguments from one declaration of the function to the next, as long as the above requirements are continuously satisfied.

For example, you can declare a function with no default arguments

void foo(int a, int b); 

In order to call that function after such declaration you'll have to specify both arguments explicitly.

Later (further down) in the same translation unit, you can re-declare it again, but this time with one default argument

void foo(int a, int b = 5); 

and from this point on you can call it with just one explicit argument.

Further down you can re-declare it yet again adding one more default argument

void foo(int a = 1, int b); 

and from this point on you can call it with no explicit arguments.

The full example might look as follows

void foo(int a, int b);  int main() {   foo(2, 3);    void foo(int a, int b = 5); // redeclare   foo(8); // OK, calls `foo(8, 5)`    void foo(int a = 1, int b); // redeclare again   foo(); // OK, calls `foo(1, 5)` }  void foo(int a, int b) {   // ... } 

As for the code in your question, both variants are perfectly valid, but they mean different things. The first variant declares a default argument for the second parameter right away. The second variant initially declares your function with no default arguments and then adds one for the second parameter.

The net effect of both of your declarations (i.e. the way it is seen by the code that follows the second declaration) is exactly the same: the function has default argument for its second parameter. However, if you manage to squeeze some code between the first and the second declarations, these two variants will behave differently. In the second variant the function has no default arguments between the declarations, so you'll have to specify both arguments explicitly.

like image 27
AnT Avatar answered Oct 01 '22 12:10

AnT