+ 1

What's wrong with this code?? Is it necessary to set default value to each parameter, if we set any value default??

#include <iostream> using namespace std; int sum(int a=42, int b) { int result = a + b; return (result); } int main() { int x = 36; int y = 24; //calling the function with both parameters int result = sum(x, y); cout << result << endl; //calling the function without a result = sum(x,y); cout << result << endl; return 0; } its showing error that it should have default value of "b" also.

14th Jan 2017, 8:26 AM
Yashwant Kumar Sonkar
Yashwant Kumar Sonkar - avatar
5 ответов
+ 4
if you will put a default value to a parameter you need to put that variable at the final of the parameter list so it should be int sum( int b, int a = 42 ) ...
14th Jan 2017, 8:35 AM
Kawaii
Kawaii - avatar
+ 2
ya rte radhika
18th Feb 2017, 1:43 PM
Tarun Verma
Tarun Verma - avatar
+ 1
a=42
19th Feb 2017, 3:17 AM
Tarun Verma
Tarun Verma - avatar
+ 1
radhika where u from
19th Feb 2017, 3:45 AM
Tarun Verma
Tarun Verma - avatar
0
no , its not necessary but actually if you are giving value to a parameter then it is must to also give the value to the parameter in right side
17th Feb 2017, 2:16 PM
Radhika Bhatia
Radhika Bhatia - avatar