3

I came across with a confusing question during an examination. Please help me to understand this concept. Code snippet is including here :

void xyz(int a = 0, int b, int c = 0)
{
    cout << a << b << c;
}

Now the question is which of the following calls are illegal?

(Assume h and g are declared as integers)

(a) xyz();    (b) xyz(h,h);

(c) xyz(h);    (d) xyz(g,g);

Codes:

(1) (a) and (c) are correct (2) (b) and (d) are correct

(3) (a) and (b) are correct (4) (b) and (c) are correct

I tried to compile the code in C++ and I got this error:

error:expected ';',',' or ')' before '=' token
void xyz (int a = 0, int b = 0, int c = 0)

Help me understand the concept.

msc
  • 33,420
  • 29
  • 119
  • 214
runner
  • 591
  • 2
  • 7
  • 20
  • Is this C or C++? Default arguments. And `a` can't have a default argument if `b` (and `c`) doesn't have one. That's your error. – J...S Oct 09 '17 at 06:46
  • 2
    Possible duplicate of [Default value of function parameter](https://stackoverflow.com/questions/2842928/default-value-of-function-parameter) – Gaurav Pathak Oct 09 '17 at 06:49

3 Answers3

10

According to cppreference:

In a function declaration, after a parameter with a default argument, all subsequent parameters must :

  • have a default argument supplied in this or a previous declaration; or
  • be a function parameter pack.

Means

void xyz(int a = 0, int b, int c = 0) // Not valid
{
   //your code
}

It is give an error because a has default value, but b after it doesn't have default value. The ordered of function declarations with default argument must be from right to left.

So, use

void xyz(int a = 0, int b=0, int c = 0) // Not valid
{
   //your code
}

Let's see some c++ example:

case 1: Valid, trailing defaults

void xyz(int a, int b = 2, int c = 3)
{
   //your code
}

case 2: Invalid, leading defaults

void xyz(int a = 1, int b = 2, int c)
{
      //Your code
}

case 3: Invalid, default in middle

void xyz(int a, int b = 3, int c);  
{
      //Your code
}
msc
  • 33,420
  • 29
  • 119
  • 214
  • 1
    Thanks for the reply. Let's say all arguments are initialized, is it possible to call the function as mentioned in the options.. eg: xyz(h,h) – runner Oct 09 '17 at 06:53
  • It may be worth mentioning the case where such a declaration is valid if preceded by another declaration that already sets the default value for `b`. – CinCout Oct 09 '17 at 06:58
  • `void xyz(int a, int b = 0, int c = 0);` and then `void xyz(int a = 0, int b, int c);` is a valid use case, missing in the answer. – CinCout Oct 09 '17 at 07:19
  • 1
    @CinCout That's right, but `xyz(int a = 0, int b, int c = 0)` is not valid – DAle Oct 09 '17 at 07:21
  • 1
    Definitely. But the missing use case makes the answer incomplete IMO. – CinCout Oct 09 '17 at 07:25
  • @rsp, all of your latter examples can be valid https://stackoverflow.com/questions/44818513/c-adding-and-redefinition-of-default-arguments-in-real-world – DAle Oct 09 '17 at 07:30
  • @DAle If I use individually function in my program, it's give an error. But, If I use all above function in my program then it is valid.Here, OP asked only about single function. – msc Oct 09 '17 at 07:35
  • @rsp, Maybe it would be better to use function definitions (as in the question), not declarations? – DAle Oct 09 '17 at 07:40
  • @DAle i did some modification. Thanks. – msc Oct 09 '17 at 07:45
2

Put default assignment in right.

void xyz(int a , int b= 0, int c = 0)
{
    count <<a<<b<<c;
} 

call it like this:

xyz(2,3); 
xyz(2,3,5);
Farhad
  • 4,119
  • 8
  • 43
  • 66
1

I think it's wrong definition of the function.

void xyz(int b, int a = 0,  int c = 0)

or

void xyz(int a = 0, int b = 0,  int c = 0)

could be ok.

distant1219
  • 78
  • 1
  • 1
  • 5