Have you ever written a formula in MS Excel and received a warning about a recursive function? In other words, when there's a recursive function, the cell holding the formula is also trying to use itself as part of the formula. In programming, a function can call itself. This may sound like a disaster waiting to happen, but it's a very useful tool. As long as you provide a way out in the recursive function, it's a handy tool.
There are times when writing a recursive function would save a lot of lines of code. Let's start by looking at factorials. In mathematics, a factorial is written as n!, and is written as follows:
Factorial formula for n:
n! = n(n - 1)(n - 2) . . . 2. .1
A factorial means that you start at the number and keep multiplying each integer by itself. For example, 5! = 5 * 4 * 3 * 2 * 1, which equals 120. Smaller numbers are fairly straightforward, if you used a simple calculator.