There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
public static int recursiveFactorial(int n) {
if (n > 0) {
System.out.println("n = " + n);
return n * recursiveFactorial(n - 1);
} else {
return 1;
}
How does it know/remember every loop through recursiveFactorial() what the previous return value was? I feel like I understood how this was supposed to work until I saw this example. Now, it makes less sense than it did earlier in the lesson. Is the previous value returned stored in the… stack… abstracted away? From what I can tell, it looks like it should just be returning:
4 * 3 = 12,
3 * 2 = 6,
2 * 1 = 2,
1 * 1 = 1
But it doesn’t appear to have the previous value stored to multiply by… how is it supposedly doing instead:
Suppose the initial argument to the recursiveFactorial method is 4. The argument 4 will be assigned to the parameter n. The if condition will be true and after the println, we reach the return statement,
return n * recursiveFactorial(n - 1);
The method will try to evaluate the expression so that it can be returned,
return 4 * recursiveFactorial(3);
But the expression includes a call to a method, so the current method says “hold everything”. I am going to pause because I can’t continue/I can’t return without knowing the result of this method call. So, the method pauses, the current state of the method goes on to the stack and a new call is made to recursiveFactorial.
Now, a fresh call is made to recusiveFactorial with an argument of 3. Once again, the method can’t return unless it knows the value of return 3 * recursiveFactorial(2);. So, now this method also pauses. The state of this method is added on top of the stack, and then the call recursiveFactorial(2) is made. At this point, we have paused two method calls. None of the methods has executed to completion yet.
A fresh call is made to recusiveFactorial with an argument of 2. Once again, the method can’t return unless it knows the value of return 2 * recursiveFactorial(1);. So, now this method also pauses. The state of this method is added on top of the stack, and then the call recursiveFactorial(1) is made. At this point, we have paused three method calls. None of the methods has executed to completion yet.
A fresh call is made to recusiveFactorial with an argument of 1. Once again, the method can’t return unless it knows the value of return 1 * recursiveFactorial(0);. So, now this method also pauses. The state of this method is added on top of the stack, and then the call recursiveFactorial(0) is made. At this point, we have paused four method calls. None of the methods has executed to completion yet.
A fresh call is made to recusiveFactorial with an argument of 0. Now finally, the if condition is not met and we reach the base case in the else block i.e. we reach the statement return 1. The method doesn’t need any additional information from some other method, so it simply returns 1.
At the top of the stack is the method which was paused when it reached the statement return 1 * recursiveFactorial(0);. Well, now it knows the return value of recursiveFactorial(0) and so the calculation simplifies to return 1 * 1. The method completes execution by returning 1. The method is popped off the stack. In this way, the returns cascade through the stack and the methods are taken off the stack as one by one each method on the stack runs to completion.
The next method on the stack was paused at the statement return 2 * recursiveFactorial(1); So, a value of 2 will be returned. And so on till the stack is emptied.
Have a look at this (there is a step by step visualization of the stack at the bottom of the page):
Because we are pausing so many method calls and adding them to the stack, so it incurs a memory cost. If we recurse too deeply, it is possible that we may end up overflowing the stack. So, the depth of the recursion is a factor when deciding if recursion is a suitable candidate for solving a problem.
This connected the dots that I needed connected in my mind. Thank you so much for this explanation. As soon as you said the first one pauses (and gets put into the stack) I was like: “Ohhhhh!” - thank you.