Recursion is really easy to understand. It is essentially a function calling itself. Just imagine writing a function that calculates the factorial of a number n (n!
). Lets see how it would expand for 4!
. Remember that 1! = 1
.
(factorial 4)
(4 * (factorial 3))
(4 * (3 * (factorial 3)))
(4 * (3 * (2 * (factorial 1))))
(4 * (3 * (2 * 1)))