Find the error(s) in each of the following program segments and explain how the error(s) can be
corrected:
(i) int function1()
{
cout << "Inside function function1 " <<
endl; int function2()
{
cout << "Inside function function1 " << endl;
}
}
(ii) int sum(int x, int y)
{
int result;
result = x +
y;
}
(iii) int computeProd(int n)
{
if (n == 0)
return 0;
else
}
n * computeProd(n – 1);
(iv) void aFunction(float a)
{
float a;
cout << a << endl;
}
(v) void theProduct()
{
int
a;
int
b;
int
c;
int result;
cout << “Enter three integers “ <<
endl; cin >> a >> b >> c;
result = a * b * c;
cout << “Result is “ << result <<
endl; return result;
}
(vi) float calculateSquare(float number)
{
return number * number;
}
#include <iostream>
using namespace std;
int function2(){
cout << "Inside function function1" << endl;
return 0; // function type INT, so it has to return integer value;
}
int function1(){
cout << "Inside function function1 " <<endl;
function2(); // do not nest functions inside functions
return 0; // function type INT, so it has to return integer value;
}
int sum(int x, int y){
int result;
result = x +y;
return result; // return the result
}
int computeProd(int n){
if (n == 0)
return 1; // if you return 0, everything will be multiplied by 0
else
return n * computeProd(n - 1);
}
void aFunction(float a){
// float a; already declared
cout << a << endl;
}
int theProduct(){ // function type void returns nothing. You need to return int.
int a; int b; int c;
int result;
cout << "Enter three integers" <<endl;
cin >> a >> b >> c;
result = a * b * c;
cout << "Result is"<< result << endl;
return result;
}
float calculateSquare(float number){ //works just fine
return number * number;
}
Comments
Leave a comment