Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying default parameter when calling C++ function

Tags:

Suppose I have code like this:

void f(int a = 0, int b = 0, int c = 0) {     //...Some Code... } 

As you can evidently see above with my code, the parameters a,b, and c have default parameter values of 0. Now take a look at my main function below:

int main() {    //Here are 4 ways of calling the above function:    int a = 2;    int b = 3;    int c = -1;     f(a, b, c);    f(a, b);    f(a);     f();    //note the above parameters could be changed for the other variables    //as well. } 

Now I know that I can't just skip a parameter, and let it have the default value, because that value would evaluate as the parameter at that position. What I mean is, that I cannot, say call, f(a,c), because, c would be evaluated as b, which is what I don't want, especially if c is the wrong type. Is there a way for the calling function to specify in C++, to use whatever default parameter value there is for the function in any given position, without being limited to going backwards from the last parameter to none? Is there any reserved keyword to achieve this, or at least a work-around? An example I can give would be like:

f(a, def, c) //Where def would mean default. 
like image 325
Arnav Borborah Avatar asked Aug 06 '16 18:08

Arnav Borborah


People also ask

Can C functions have default parameters?

Yes. :-) But not in a way you would expect. Unfortunately, C doesn't allow you to overload methods so you'd end up with two different functions. Still, by calling f2, you'd actually be calling f1 with a default value.

How do you declare a default argument in C?

Parameters with default arguments must be the trailing parameters in the function declaration parameter list. For example: void f(int a, int b = 2, int c = 3); // trailing defaults void g(int a = 1, int b = 2, int c); // error, leading defaults void h(int a, int b = 3, int c); // error, default in middle.

Can you assign the default values to a function parameters?

Default parameter in JavascriptThe default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

How are parameters passed by default in C?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.


2 Answers

There isn't a reserved word for this, and f(a,,c) is not valid either. You can omit a number of rightmost optional parameters, as you show, but not the middle one like that.

http://www.learncpp.com/cpp-tutorial/77-default-parameters/

Quoting directly from the link above:

Multiple default parameters

A function can have multiple default parameters:

void printValues(int x=10, int y=20, int z=30) {     std::cout << "Values: " << x << " " << y << " " << z << '\n'; } 

Given the following function calls:

printValues(1, 2, 3); printValues(1, 2); printValues(1); printValues(); 

The following output is produced:

Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30 

Note that it is impossible to supply a user-defined value for z without also supplying a value for x and y. This is because C++ does not support a function call syntax such as printValues(,,3). This has two major consequences:

1) All default parameters must be the rightmost parameters. The following is not allowed:

void printValue(int x=10, int y); // not allowed 

2) If more than one default parameter exists, the leftmost default parameter should be the one most likely to be explicitly set by the user.

like image 149
Alejandro Avatar answered Oct 29 '22 19:10

Alejandro


As workaround, you may (ab)use boost::optional (until std::optional from c++17):

void f(boost::optional<int> oa = boost::none,        boost::optional<int> ob = boost::none,        boost::optional<int> oc = boost::none) {     int a = oa.value_or(0); // Real default value go here     int b = ob.value_or(0); // Real default value go here     int c = oc.value_or(0); // Real default value go here      //...Some Code... } 

and then call it

f(a, boost::none, c); 
like image 41
Jarod42 Avatar answered Oct 29 '22 18:10

Jarod42