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 2 of 2.

Varun Girdhar said:   1 decade ago
There are two rules you must be aware of when using default arguments. First, only trailing arguments may be defaulted. That is, you can't have a default argument followed by a non-default argument. Second, once you start using default arguments in a particular function call, all the subsequent arguments in that function's argument list must be defaulted (this follows from the first rule).

Anurag Kumar said:   1 decade ago
During prototype declaration of a function default argument should be in right hand side in the parameter list.

Manoj Tiwari said:   1 decade ago
Correct Answer is A, not D. Please refer page No. 132 of "The C++ Programming language" by Bjarne Stroustrup. It states that: It is possible to provide default arguments for trailing arguments only.

MAYSON KALARIKKAL 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.
Eg:
int f(int a, int b=10,int y=5)

when we call this function with f(25,20)
we will get a=25,b=20 and y=5

Satyanarayana said:   1 decade ago
Default arguments must be at trailing side only or all arguments must be default arguments.


Post your comments here:

Your comments will be displayed after verification.