I did a exercise,but got a wierd result.
Here is the question:
Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them. Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
Here is my solution:
Code:
#include <iostream>
using namespace std;
int calculate(int x,int y,int *sum)
{
*sum = x + y;
return x*y;
}
int main()
{
int num1,num2,sum;
cout<<"enter number 1:";
cin>>num1;
cout<<"enter number 2:";
cin>>num2;
cout<<calculate(num1,num2,&sum)<<endl<<sum;
return 0;
}
If I put num1=4 and num2=5,It would cout 20 .
4309744
Where is 4309744 coming from?
Is that address of variable sum? But I think my sytanx is right.
Any help would be appreciated.