Reputation: 543
I was playing around with this function:
public class x {
public static void main(String[] args) {
recurse(10);
}
public static int recurse(int theNumber) {
if(theNumber == 0) {
return 0;
}
else
{
System.out.println(theNumber);
theNumber--;
recurse(theNumber);
System.out.println(theNumber);
}
return -1;
}
}
and I got this output:
10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 Press any key to continue . . .
How is this possible? I understand where the countdown from 10 to 0 comes from.. but how on earth does it count back up?? I'm quite certain I'm missing a fundamental concept about recursion. Can someone fill in the blanks??
Upvotes: 2
Views: 234
Reputation: 2112
Habib explained it quite well actually. Just make sure you understand that the recursion is followed until the parameter theNumber is zero, hence the recursion terminates, and that this happens before any of the the last Sysout calls is done. Also keep in mind that the variable is not shared but local to each invocation. Visually, it might look like this
recurse(5)
print(5)
recurse(4)
print(4)
recurse(3)
print(3)
recurse(2)
print(2)
recurse(1)
print(1)
recurse(0)
print(0)
print(1)
print(2)
print(3)
print(4)
Calls that are in the same column belong to the same stackframe, i.e. same method invocation. Notice, how you decent in the call hierarchy before you actually reach the first print statement after the recursive invocation. This should make it clear, to you.
Upvotes: 3
Reputation: 1719
Before your last syso statement you are calling recurse function so it is going on adding your calls on stack..and after reaching value to 0 it is returning to current function back by pulling values from stack.. and after that it is printing values by your last syso statement...
Upvotes: 0
Reputation: 1105
For brevity, let's say you call recurse(2)
in your main program. Then you have:
theNumber
= 2 prints 2, decrements, calls recurse(1)
--> theNumber
= 1, prints 1, decrements, calls recurse(0)
----> theNumber
= 0, returns without printing
--> theNumber
= 1 prints 0 (decremented from 1), returns
theNumber
= 2 prints 1 (decremented from 2), returns
Upvotes: 2
Reputation: 4202
This is what you need-
public static int recurse(int theNumber) {
System.out.println(theNumber);
if (theNumber > 0) {
theNumber--;
recurse(theNumber);
}
return 0;
}
Upvotes: 1
Reputation: 1467
As Habib said flow goes into recursion and then after completion of executing recursive function your last System.out.println(theNumber) gets called.
----> public static int recurse(int theNumber) {
|
| if(theNumber == 0) {
| return 0;
| }
|
| else
| {
| System.out.println(theNumber);
| theNumber--;
----------< recurse(theNumber);
System.out.println(theNumber);
}
return -1;
}
So the time when function returns from recursion theNumber contains the value which was was decremented by 1 only.
Upvotes: 0
Reputation: 2289
Your last System.out.println(theNumber)
is the reason of it. As you were asking about the Flow. First the recursive call is completed and then it's making BackTracking. Check this Link BackTracking
Upvotes: 0
Reputation: 223267
That is because of the last System.out.println(theNumber);
in your method. After recursively calling the method, and upcon completion of the method it is printing those values.
Upvotes: 6