Why default argument should be at end in c++?
Answers
Answered by
1
When you defines a function with default arguments the compiler checks for arguments from right to left and assigns the values passed from calling function from right to left. Suppose you defined a function,
void myFunc(int a=0, int b=0, int c);
When you call this function with arguments like,
myFunc(3,4);
the compiler assigns 4 to c. Now in which parameter the argument 3 will be passed? a or b?? The compiler here generates an error.
error: default argument missing for parameter 3 of 'void myFunc(int, int, int)
This is why we assigns default arguments from right to left and default arguments are thete at the end.
void myFunc(int a=0, int b=0, int c);
When you call this function with arguments like,
myFunc(3,4);
the compiler assigns 4 to c. Now in which parameter the argument 3 will be passed? a or b?? The compiler here generates an error.
error: default argument missing for parameter 3 of 'void myFunc(int, int, int)
This is why we assigns default arguments from right to left and default arguments are thete at the end.
Similar questions