Reputation: 1211
public int recursive(int x){
if(x>10){
return x;
}
int y = x + 1;
int r = recursive(y);
System.out.println(y + " Y" + " R" + r + " X " + x);
return r;
}
public static void main(String[] args) {
System.out.println(new a().recursive(1));
}
This is a recursion I made to simplify a different method I could not understand but it has the same code line for line basically. I don't understand what is happening on this line int r = recursive(y);
. I don't understand what the return r;
even returns to or how it actively loops. When I system print R, it is always the same value each iteration which throws me off. If I change return r to return 555
the code still works but just returns 555
to the call from main.
output
11 Y R11 X 10
10 Y R11 X 9
9 Y R11 X 8
8 Y R11 X 7
7 Y R11 X 6
6 Y R11 X 5
5 Y R11 X 4
4 Y R11 X 3
3 Y R11 X 2
2 Y R11 X 1
11
Please eli5, I have viewed some recursion videos but they do not use a recursion thats laid out like this, with a return after a recursive call. int r = recursive(y);
, how does r
get its value here, and continue the loop, can't figure it out. It says set r
equal to the return of recursive(y)
, but the return is r
itself when the value for r
has not been set or something?
Another issue I am having is how the system print is running as well, because I thought when the recursive part runs, it instantly restarts the method but it seems the entire method is running and looping
Upvotes: 0
Views: 813
Reputation: 1717
My friend it looks to me that your recursion breaking conditions is at r>10 so it always breaks at 11 thats the only cause............
suppose you have pass x=1 then your recursive(1) calls recursive(2), recursive(2) calls recursive(3), recursive(3) calls recursive(4) , recursive(4) calls recursive(5) , recursive(5) calls recursive(6), recursive(6) calls recursive(7), recursive(7) calls recursive(8), recursive(8) calls recursive(9), recursive(9) calls recursive(10), recursive(10) calls recursive(11)
and then recursion breaks and at recursive(11) it returns you 11 at this line `int r = recursive(y); so r becomes 11 and then recursion(10) returns you r and then recursion(9) returns you are and so on ................. for whole stack
Upvotes: 1
Reputation: 225
When you enter 555, the first condition you have in your function is to return the value if it is greater than 10, so as 555 is greater than 10, it returns directly the value (555).
Now, for the recursion. It's pretty simple, just imagine how the code flows, you enter 1 as a parameter, is it greater than 10? No, so we move to the next line. We assign y the value of x plus 1, so X=1, and Y=2.
And then instead of returning a value, we call again the same function, but instead of 1 as a parameter, we call it with 2 as a parameter (the value of Y). And we start again (that's the point of recursion), but now X=2: Is 2 greater than 10? Now, so we move to the next line. We assign Y the value of the parameter (which is called X) plus 1, so now Y=3 and X=2. Remember that the variables' scope are only in the same function they are declared.
Repeat this, until the parameter goes above 10 (by adding one each time you call the recursive function), and then finally it returns a value! (in the condition where x > 10), so the function finishes and returns the value to the function who called it, which happens to assign that value to R and prints the result.
And so on, finishing each of the called functions that had different values as a parameter (1, 2, 3, etc until 10, as the function each time added one to the parameter and called itself).
I hope this helped you to understand it better, I'm not sure how to explain this simplier. I guess a 5 year old would have some problems to understand this :)
Upvotes: 1
Reputation: 2373
You can think of it like a simple loop in this case, it just iterates untill x
is >
10, so ret value of this recusrsion is 11, since it is the first number which is bigger than 10. Since nothing printed before recursive call you print the values in the reversed order, after x becomes 11, and function returns, previos stack frame contains values x == 10, y 10 + 1, and r contains return value of the functon => 11, same for previos frame and previos .....
Upvotes: 1