12 Find the errors in each of the following function segment

12. Find the error(s) in each of the following function segments, and explain how the error(s) can be corrected (10 marks) a) int sum( int x, inty) int result int sum( int n) { if(n=0) return 0; else n + sum(n -1);) b) n+ sum(n -1)) c) void product) ( inta; intb; intc; int result cout a >> b>>c result = a * b *c; cout

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;

   }

 12. Find the error(s) in each of the following function segments, and explain how the error(s) can be corrected (10 marks) a) int sum( int x, inty) int result
 12. Find the error(s) in each of the following function segments, and explain how the error(s) can be corrected (10 marks) a) int sum( int x, inty) int result

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site