so I made a function which returns a value but how would I be able to print that return value to main? p.s. sorry if my formatting is weird im new here.
int main()
{
if (options == 3)
{
(powerCalculation(base, exponent));
//cout << powerCalculation(result) << endl;
//I want to put the result here
}
return 0;
}
int powerCalculation(int base, int exponent)
{
int i = 0;
int result = base;
while (i <= exponent)
{
result = result * base;
i ;
}
return result;
}
CodePudding user response:
There are two possibilities in main.
- Save the result in main:
int result = powerCalculation(base, exponent);
std::cout << result << std::endl;
- Print the result directly without storing it
std::cout << powerCalculation(base, exponent) << std::endl;
CodePudding user response:
solution
You can directly use the function's return value when calling the function. i.e. cout << powerCalculation(2,10) << endl;
or int my_result = powerCalculation(base,exponent);
demo
int main(){
int base=2, exponent=10
cout << powerCalculation(base, exponent) << endl;
return 0;
}
int powerCalculation(int base, int exponent){
int i = 0;
int result = base;
while (i <= exponent) {
result = result * base;
i ;
}
return result;
}
CodePudding user response:
int powerCalculation(int base, int exponent)
{
int i = 0;
int result = base;
while (i <= exponent)
{
result = result * base;
i ;
}
// Add this
cout << result << endl;
return result;
}