Reputation: 103
I am new to Java, The following code computes a^b, I have compiled it on the computer and it is working fine. But I do not understand how it is working? How java compiler computes a * power(a, b - 1)
Can anyone please explain to me this code?
int power(int a, int b) {
if (b < 0) {
return 0;
} else if (b == 0) {
return 1;
} else {
return a * power(a, b - 1);
}
}
Upvotes: 0
Views: 241
Reputation: 28036
Your function is recursive. It's probably easiest to demonstrate how it works with an example.
Let's say you want to compute 2^4.
You therefore call power(2, 4);
This is how it will be evaluated (note that you have gotten the base case wrong):
power(2, 4) // b > 0, so it expands.
2 * power(2, 3)
2 * (2 * power(2, 2))
2 * (2 * (2 * power(2, 1)))
2 * (2 * (2 * (2 * power(2, 0)))) // Now, b == 0, so it evaluates to -1
2 * (2 * (2 * (2 * -1)))
2 * (2 * (2 * -2))
2 * (2 * -4)
2 * -8
-16
Upvotes: 8
Reputation: 2357
step 1 : call Power(2,2)
Step 2 : go to else part a=2 and call again power(2,1)
step 3 : goto else part a=2 and call again power(2,0)
step 4 : goto else if part return -1 and go back to step 3
step 5(step 3) : calculate 2*-1 = -2;go back to step 2
step 6(step 2) : calculate 2*-2=-4 and finally return -4
here step 2 to 4 same method call until match a specific condition . it's call Recursion
your function have some error so it return 2^2= -4
Upvotes: 0
Reputation: 1286
It is called a recursive function, that's a function that call itself. To be sure it does not call itself infinitely (and causes a stack overflow ;)), be sure to have at least one exit condition.
An exit condition is one return that does not call the function itself.
Take this example: 3^4
. In fact, 3^4
is the same that (3*3)*(4-1)
or (3*3*3)*(4-2)
or (3*3*3*3)*(4-3)
. That is exactly what you do with recursion !
In your case, the recusrive call is
return a * power(a, b - 1);
(That is doing exactly what I showed above)
And you have 2 exit conditions :
if (b < 0) {
return 0;
} else if (b == 0) {
return -1;
}
Upvotes: 1
Reputation: 40044
This is recursion. That is when a program calls itself. If you put print statements as shown you can see how it works.
static int power(int a, int b) {
if (b < 0) {
return 0;
}
else if (b == 0) {
return 1;
}
else {
System.out.println("Before: a = " + a + ", b = " + b);
int res = a * power(a, b - 1);
System.out.println(
"After: res = " + res + ", a = " + a + ", b = " + b);
return res;
}
}
Each time thru the values are altered as shown by the call to power with b being reduced by 1. Then when b == 0
, the program starts returning, "retrieving"
each value from the call stack to do the computation.
Upvotes: 1
Reputation: 103018
The code as stated gives the wrong answers; line 5 should read return 1;
, not return -1;
.
Java computes a call to the power
method by.. invoking it. This is no different here, it might just confuse you a bit because we're calling the power method from within the power method. Which in java is fine.
So, let's try power(3, 4)
as an example.
Java will first check if that 4 is below 0. It isn't, so skip that. Then if it is 0. It is not, so skip that. Then it'll return the result of the expression (filling in the variable values): return 3 * power(3, 4 - 1)
. To calculate that, power(3, 3)
must be evaluated first.
So java... does that. It remembers its half-way done work on power(3, 4)
(it does this 'on the stack') and now goes to calculate power(3, 3)
. The answer is to evaluate return 3 * power(3, 2)
. So, java remembers half of the work done for power(3, 3)
and goes to calculate that. The answer is the result of return 3 * power(3,1)
. You guessed it, remember our work and invoke power yet again: return 3 * power(3, 0)
but finally we're out: The method call power(3, 0)
is resolved by the second 'if', thus, return 1;
happens. power(3, 0)
successfully completed! Now power(3, 1)
can complete, then power(3, 2)
can complete, all the way up, and 81 is returned.
Upvotes: 1
Reputation: 5309
This code works on recursion. Each recursive call pushes a new stack frame, then pops it when it returns.
You need to check the base case in the above code. So, when b==0, it should return 1.
Upvotes: 0