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.
#include <iostream>
usingnamespace 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:
#include <iostream>
usingnamespace 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.