Quick Question.. Passing one value from main & returning two values

I am trying to call a function from main(), passing one value to it and return TWO values, both the square of the number and the number to the power of three.

I think I have the first part (because it compiles).

I am not finding any info on how to call the two values. Any help would be appreciated.

Here is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

void power(int num, int& square, int& cube)
{
square = num * num;
cube = num * num * num;
}

// ^^ I think I am good above this comment ^^

int main()
{
int number;

cout << "Type in a number ";
cin >> number;
cout << "The square is: " << power(number.square) << endl;
cout << number << " to the power of 3: " << power(number.cube) << endl;

system("pause");
return 0;
}
The best way to explain might be to just show you. Since you're passing by reference, the variables you want these values to be put into must exist at the time you call the function. Like this:

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
#include <iostream>
using namespace std;

void power(int num, int& square, int& cube)
{
square = num * num;
cube = num * num * num;
}

// ^^ I think I am good above this comment ^^

int main()
{
int number;
int squaredNumber;
int cubedNumber;


cout << "Type in a number ";
cin >> number;

// Now calculate values and store in variables
power(number, squaredNumber, cubedNumber); 

cout << "The square is: " << squaredNumber << endl;
cout << number << " to the power of 3: " << cubedNumber << endl;

return 0;
}


This power(number.square) is just not at all how to call the function. See my code above for how to call it.
Last edited on
Thank you very much for the help. I can't believe I missed that..
Topic archived. No new replies allowed.