function

what is d prblm??
#include<iostream>
using namespace std;

class test{
private:

public:

int sumf(int,int);
int sumf1(int,int);


};

int test::sumf(int x,int y)
{

int z;
z=x+y;
return z;
}
int test::sumf1(int p,int q)
{

int r,x,y;

r=(p-q)+ sumf(x,y);

return r ;


}

int main()

{
int c,a,b;
int p,q,r;
test obj;

cout<<"Enter the number of Xand Y :"<<endl;
cin>>a>>b;
c=obj.sumf(a,b);
cout<<endl;
cout<<"x+y="<<c;

cout<<"Enter the number of P and q :"<<endl;
cin>>p>>q;
r=obj.sumf1(p,q);
cout<<"(P-q)+x+y="<<r;
}
Please Edit your post and put all of your code between code tags <>. Its under the format section.

You tell us whats wrong. What errors are you getting? etc.

Just know, you have to initialize the variables in the function sumf1.

x = 0, y = 0;
Last edited on
Your compiler should, at least with suitable options, be able to make a comment similar to:
In member function 'int test::sumf1(int, int)':
  27:18: warning: 'x' is used uninitialized in this function [-Wuninitialized]
  27:18: warning: 'y' is used uninitialized in this function [-Wuninitialized] 

You, in turn, should pay attention to the feedback that the compiler gives.
when i initialize the value of x=0,y=0....this time my 2nd function returns only p-q....bt i want to return p-q+(x+y)
Thats becuase your functions doesnt make any sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int test::sumf(int x, int y)
{

	int z;
	z = x + y;
	return z;
}
int test::sumf1(int p, int q)
{

	int r, x = 0, y = 0;

	r = (p - q) + sumf(x, y);

	return r;
}


You call sumf and send in x and y. x and y are 0. Then you do z = 0 + 0. Which is still 0.

Initialize x and y to what you want it to be.
Topic archived. No new replies allowed.