Recursion

A hard concept which is easily explained using this example:

Recursion is like opening a box, and if there's another box inside, you open that one too. You keep doing this until you find a box that doesn't have any more boxes inside.

In programming, it's like solving a problem by breaking it down into smaller parts, and you keep working on those smaller parts until the problem is solved. Just like how you keep opening boxes until there's nothing left inside!

A recursive function is a function which calls itself. It classic example is calculating N factorial. N factorial is represented using N! which is calculated by taking N * (N-1) * (N-2) …. (1).

Recursion Example:

5! = 5 * 4 * 3 * 2 *1

We can calculate this with Lua like this:

function FACTorial(N) if (N == 1) then return 1 end return N * FACTorial(N-1) end function main(Data) local Result = FACTorial(5); --returns 120 end

This example uses the concept of recursion to call itself until there are no more calls to be made (No more boxes to open).

Â