12 Find the errors in each of the following function segment
Solution
a)
int sum(int x, int y){
int result;
result = x + y;
}
Error: missing return statement
This function returns a int value but currently it is not returning int value
Correct:
int sum(int x, int y){
int result;
result = x + y;
return result;
}
b)
int sum(int n){
if(n==0)
return 0;
else
n + sum(n-1);
}
Error:
missing return statement in \'else\' part.
Correct:
int sum(int n){
if(n==0)
return 0;
else
return (n + sum(n-1));
}
c)
void product(){
int a;
int b;
int c;
int result;
cout<<\"Enter three integers: \";
cin>>a>>b>>c;
result = a*b*c;
cout<<\"Result is \"<<result;
return result;
}
Error:
returning \'int\' value
THis function is declared as \'void\' so it can not return
any type of value
Correct:
void product(){
int a;
int b;
int c;
int result;
cout<<\"Enter three integers: \";
cin>>a>>b>>c;
result = a*b*c;
cout<<\"Result is \"<<result;
}

