public static void fun(int x){
	System.out.println(x);
	x--;
	if(x > 0){
		fun(x);
	}
	System.out.println("returning " + x);
			
		
}

in the main class: fun(5);

output:
5
4
3
2
1
returning 0
returning 1
returning 2
returning 3
returning 4
---------------------------------------

what i don't get is how the print statement is able to print 5 times when i think the method ends with x = 0, and the print statement only executes once after. can somebody please explain how this works??

Get a piece of paper and walk through it.

fun(5):
print 5
decrement 5
if 4>0, call fun(4)
>fun(4)
>print 4
>decrement 4
>if 3>0, call fun(3)
>>fun(3)
>>...
>>>fun(2)
>>>>fun(1)
>>>>print 1
>>>>decrement 1
>>>>0 is not > 0, so don't fun(0)

>>>>then what?
>>>then what?
>>then what?
>then what?
then what?

Edited 5 Years Ago by jon.kiparsky: n/a

You just have to carefully follow the method calls. When fun(5) is called it prints out 5, then subtracts 1 from 5 and then calls fun(4). Now it prints out 4 and calls fun(3). It does this until it reaches zero. Once it stops calling fun(x) you have to backtrack through the method calls for the second print statement.

The most recent method call was fun(1), where the value of X now equals zero so it prints out returning 0. The second most recent call was fun(2) so it prints out returning 1. And so on. Each method is never completed until you backtrack through all the other ones.

It's kind of hard to explain, try drawing out the method calls to make it easier to understand. It is basically the fundamental property of recursion.

so are you saying that the print statements that were ignored initially are going to be executed once fun(); stops being recursive?

They weren't ignored, and it doesn't stop being recursive.

When you call function x, you do a thing(1), then you do another thing(2), then if a certain condition holds you do another thing(3). Then when you're done with the other thing(3), you print out a line (4) and then you're done.

Now it turns out that the third thing you do is to call a function, call it function q.

In function q, you do a thing(1), then you do another thing(2), then if a certain condition holds you do another thing(3). Then when you're done with the other thing(3), you print out a line (4) and then you're done, so you go back to function x.

Now it turns out that the third thing you do in function q is to call a function, call it function r.

In function r, you do a thing(1), then you do another thing(2), then if a certain condition holds you do another thing(3). Then when you're done with the other thing(3), you print out a line (4) and then you're done, so you go back to function q.


Try writing it out as three separate functions, with the same code and different names. x(3) calls q(2) calls r(1) would call s(0) except 0 is not >0, so it finishes up and returns to q, finishes up there and returns to x, finishes up there and returns to x, and you're done.

Now just collapse the three functions into one, and it's the same thing.

This question has already been answered. Start a new discussion instead.