Welcome to the beautifull world of recursion. It may be a hard concept to grasp, but very rewarding when you finally understand it.
@Elif A have written a nice table which shows exactly how the program run. However, when I learned recursion myself, I had this strategy where I "mapped" all the inputs on a piece of paper, starting with the inputs which gave me a value, instead of a function call. Then I build my way up. I really recommend this strategy, if you have a hard time understanding recursion.
Consider the following code
function factorial(n) {
if (n == 1) {
return 1;
}
else {
return n*factorial(n-1)
}
}
Lets say we want to find factorial(5). Instead of starting from the top, evaluating factorial(5), lets start at the bottom, building our way up to factorial(5). You'll see why this is a good intuitive way of understanding recursion.
factorial(1) = 1
factorial(2) = 2 * factorial(1) = 2 * 1 = 2
factorial(3) = 3 * factorial(2) = 2 * 3 = 6
factorial(4) = 4 * factorial(3) = 4 * 6 = 24
factorial(5) = 5 * factorial(4) = 5 * 24 = 120
Again, let me precise that this is just a way of understanding recursion. The table, which I mentioned is how the program actually run, and do the recursion.