Lets break it down, lets see what happens when we try factorial(3).
We call factorial(3), and since 3 is larger than 1, it returns 3*factorial(3-1).
but before we get an answer we need to calculate the factorial of 3-1 (2). Lets do that.
we call factorial(2), and since 2 is larger than 1, it returns 2*factorial(2-1).
So now we need to calculate the factorial of 2-1 (1). This would be
We call factorial(1), and since 1 is equal to 1, it returns 1.
So moving backwards, we have:
factorial(1) = 1;
factorial(2) = 2*factorial(1) = 2*1;
factorial(3) = 3*factorial(2) = 3*2*factorial(1) = 3*2*1;
Make a bit more sense? This is the way recursive functions work, they call themselves multiple times with different numbers to get a final answer.