C++ Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 9)
9.
Which of the following function declaration is/are incorrect?
int Sum(int a, int b = 2, int c = 3);
int Sum(int a = 5, int b);
int Sum(int a = 0, int b, int c = 3);
Both B and C are incorrect.
All are correct.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 1 of 2.

Santosh kumar yadav said:   11 months ago
I think inc sum(int a = 0 ,int b int c =3) is the correct answer.

Dharmi said:   5 years ago
Why option B is incorrect?

Mirza Waseem Hussain said:   5 years ago
If you assign default value to an argument, the subsequent arguments must have default values assigned to them, else you will get compilation error.
(3)

Bipul said:   8 years ago
Here we have to find which are incorrect that's why option B and C are in correct. Option D is the answer. And option A is correct.

Raksha G said:   9 years ago
Correct answer is A because default arguments should start from right.
(2)

Nilesh Chavan said:   9 years ago
But here we have to find the incorrect answer so b & c both are incorrect.

Hence, the correct answer is C.

Abreham said:   10 years ago
What is the difference between function definition & function declaration?

Roopam said:   1 decade ago
Correct answer is A. Because in default argument we give value from right to left in function argument that's why D is wrong.

Ex.
int sum(int a, int b=2 ,int c=8);
is right?

int sum(int a=3, int b, int c=4);
is wrong?
(1)

Sonu Rajpoot said:   1 decade ago
Arguments are defaulted from right to left but c++ expects that only the arguments on the right hand side can be defaulted.

Only trailing arguments may be defaulted or all arguments may be defaulted.

Ex:

void show(int a, int b=10, int c=20)
void show_1(int a=1, int b=3, int c=5)

Vinayak Naik said:   1 decade ago
My question goes to @Varun Girdhar, first rule is understood but in the second rule there is a query. As you can see in option C, default argument int b is followed by non-default argument int c = 3. So that contradicts rule 2 of all the subsequent arguments in that function's argument list must be defaulted (this follows from the first rule). Correct me if I am wrong.


Post your comments here:

Your comments will be displayed after verification.