Two Values From a Function

Write your question here.
The problem was to write a function that takes two arguments and provide two separate results. The code below works. However, is this the way it should be done?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  /*
*	Chapter 13 Problem 4
*
*	Write a function that takes two 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.
*
*	Use a pointer inside the function to return the results of 
*	the addition.
*/

#include <iostream>

using namespace std;

//Function Prototype
int MulAdd(int* x, int* y);

int main()
{
	// Declare and initialize variables
	int x = 0;
	int y = 0;
	int* p_x = &x;
	int* p_y = &y;


	//	Ask user for x and y values
	cout << "Please enter a value for x: ";
	cin >> x;
	cout << "Please enter a value for y: ";
	cin >> y;
	int m_result = MulAdd(p_x, p_y);
	cout << "x * y = " << m_result << "; x + y = " << x << endl;;;;
}

int MulAdd(int* x, int* y)
{
	int temp = *x;
	*x = temp + *y;

	int result = 0;
	return result = temp * *y;
}
Last edited on
You haven't written a function that takes two arguments - which is what you were explicitly asked to do.
Aren't int* x and int* y the two arguments in the MulAdd function?
@phztfte1 - yes they are, but it looks like you edited it 2 mins after MikeyBoy posted :)

In your code above i would remove p_x and p_y
and call like this
int m_result = MulAdd(&x, &y);

and your function is a little confusing...
1
2
3
4
5
6
int MulAdd(int* x, int* y)
{
	int mul= *x * *y;
        *x += *y;  
	return mul;
}
closed account (j3Rz8vqX)
Some possibilities:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

using namespace std;

//Function Prototype
void MulAdd(int x, int y, int &product, int &sum);
int *MulAdd(int x, int y);
/*
*	Chapter 13 Problem 4
*
*	Write a function that takes two 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.
*
*	Use a pointer inside the function to return the results of
*	the addition.
*/
int main()
{
	// Declare and initialize variables
	int x = 0;
	int y = 0;

	//	Ask user for x and y values
	cout << "Please enter a value for x: ";
	cin >> x;
	cout << "Please enter a value for y: ";
	cin >> y;

	//Option 1: No return: Reference, new variables;
	int product, sum;
	MulAdd(x,y,product,sum);
	cout << "x * y = " << product << "; x + y = " << sum << endl;

	//Option 2: Return by pointer to array[2];
	int *m_result =	MulAdd(x, y);
	cout << "x * y = " << m_result[0] << "; x + y = " << m_result[1] << endl;
}

//Possible referenced parameter: 4 arguments:
void MulAdd(int x, int y, int &product, int &sum)
{
    product = x*y;
    sum = x+y;
}

//Possible pointer return:
int* MulAdd(int x, int y)
{
    static int arry[2];
    arry[0] = x*y;
    arry[1] = x+y;
    int *results = arry;
    return results;
}
Please enter a value for x: 5
Please enter a value for y: 10
x * y = 50; x + y = 15
x * y = 50; x + y = 15
Last edited on
yes they are, but it looks like you edited it 2 mins after MikeyBoy posted :)

Indeed. That function was edited in to the OP, after I posted.
MikeyBoy,

My apologies. After posting, I read my post and saw that I did not include the function. I had to open up my program, copy the function, and then paste it into the posted code. I did not see your post. We were probably doing our things simultaneously. In any event, it's pretty funny.

Jaybob66,

Your method is cleaner and simpler. It is the way it should be done.

Dput,

It looks like the int* function might work. I have not yet learn what "static" does.

Thank you all.

Topic archived. No new replies allowed.