homework question for functions

I have this question to answer:

What is wrong with the following function?
void power (int x, int y, int result)
{
result = 1;
while (y > 0)
{
result = result * x;
y --;
}
}

I believe it's because the while loop is not with the void power (int x, int y, int result). Doesn't the while loop have to be with that in order for the loop to work? Any help or input would be appreciated.

Many thanks!
No, that isn't the issue.

I can think of one main thing wrong with that function...think about what it is supposed to do and how it would work if you used it in a program.
Well it wouldn't print anything to the screen, since it's void.

Edit: that's pretty much the only thing I would see. So, the loop wouldn't even print out to the screen. It would just be..nothing. Right?
Last edited on
If you want it to prin out something you have to do a reutn statement.
You don't need int result in the function declaration you need to put int before result = 1;
looks to me like its trying to be a tail recurive function. My teacher didnt go over them closely. But the idea was the answer was passed down through each step. but i think you would need something like this.

void power (int x, int y, int& result)


so pass in result by reference. it would still be a void function though
Ah I see btripp. So, if you put the int& result in there, it pretty much means the argument and parameter aren't the same copy. It can change. Right?
If you put a & in there, it means that the argument and parameter are the same. With int& result, when you modify result, you're modifying the variable that was actually passed to the function, whereas without that you're modifying copies, which the function caller will never see.

In other words:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void power (int x, int y, int result)
{
	result = 1;
	while (y > 0)
	{
		result = result * x;
		y --;
	}
}

int main()
{
	int result;
	power (2, 2, result);
	cout << result;
	return 0;
}


Will do nothing, because the 'result' variable in main() is never modified by power() (it just made it's own copy and modified that) while:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void power (int x, int y, int& result)
{
	result = 1;
	while (y > 0)
	{
		result = result * x;
		y --;
	}
}

int main()
{
	int result;
	power (2, 2, result);
	cout << result;
	return 0;
}


Will return 4, because the 'result' variable in power() is actually the same thing as the one in main(), not a copy.

Generally though, we wouldn't use references like this. It's much easier and expected to do it with return values:
1
2
3
4
5
6
7
8
9
10
11
int power (int x, int y)
{
	result = 1;
	while (y > 0)
	{
		result = result * x;
		y --;
	}
	
	return result;
}


This way we can use the main() function like this:
1
2
3
4
5
int main()
{
	cout << power(2, 2);
	return 0;
}


And we would get "4".
Ah, okay. Yes I'm more familiar with the bottom two.
yea i wouldnt use a reference in this case, i just thought it was required for the assignment. Glad you see whats going on now
Topic archived. No new replies allowed.