C++ Programming - Functions - Discussion
Discussion Forum : Functions - General Questions (Q.No. 17)
17.
Which of the following statement is correct?
Discussion:
6 comments Page 1 of 1.
Adarsh said:
1 year ago
According to me, The order of the default argument will be left to right.
Yashi said:
5 years ago
Thanks @Musaddique.
Vidhya said:
7 years ago
excellent session, Thanks @Musaddique.
Boriifan gaaddisaa said:
9 years ago
Amazing! @Musaddique.
ANkit said:
10 years ago
Order will be right to left.
Musaddique said:
1 decade ago
void PrintValues(int nValue1, int nValue2=10)
{
using namespace std;
cout << "1st value: " << nValue1 << endl;
cout << "2nd value: " << nValue2 << endl;
}
int main()
{
PrintValues(1); // nValue2 will use default parameter of 10
PrintValues(3, 4); // override default value for nValue2
}
output
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4
In the first function call, the caller did not supply an argument for nValue2, so the function used the default value of 10. In the second call, the caller did supply a value for nValue2, so the user-supplied value was used.
{
using namespace std;
cout << "1st value: " << nValue1 << endl;
cout << "2nd value: " << nValue2 << endl;
}
int main()
{
PrintValues(1); // nValue2 will use default parameter of 10
PrintValues(3, 4); // override default value for nValue2
}
output
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4
In the first function call, the caller did not supply an argument for nValue2, so the function used the default value of 10. In the second call, the caller did supply a value for nValue2, so the user-supplied value was used.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers