How to omit certain arguments for which default values were given in the function's prototype parameters?

1

How to omit specific function arguments in C ++ for which standard values were given in function prototype parameters?

For example, how to omit the second argument (b = 4 by default), passing the first and last?

#include <iostream>
using namespace std;

int SOMA(int a=2, int b=4, int c=5)
{
  int s;
  s=a+b+c;
  return (s);
}

int main ()
{
    //b e c omissos
  cout << SOMA(1) << '\n'; //10
    //b omisso
  cout << SOMA(1,,2) << '\n'; //erro

  return 0;
}
    
asked by anonymous 05.04.2017 / 12:23

2 answers

2

There are even some techniques to allow this but everything so complicated, needs so much work and in general has cost of processing that is not worth the effort, it is much simpler to use the value.

C ++ does not allow an argument to be omitted in the middle, after omitting one argument, all of the following must be omitted as well. And of course that after having a parameter with default value can not have one without a default value.     

05.04.2017 / 13:06
0

Standard Arguments for Functions

C ++ allows a function to bind a default value to a parameter when no corresponding argument is used in the call    of the function.

void f (int i = 1)
{
.
.
}

In the call if you use

f(10) // recebe 10 como parâmetro
f() // recebe 1 que é o valor default.

Another example:

void myfunc (int i=5, double d=1.23);
myfunc (12, 3.45) // usara os valores 12 e 3.45
myfunc (3) // usara como myfunc( 3, 1.23)
myfunc () // usara como myfunc(5, 1.23)
  

It is not allowed to omit the first parameter and pass only the second. Only   can be omitted from right to left. If you have 5 parameters to   omitting the third, the fourth and fifth should also be omitted.

    
05.04.2017 / 13:12