Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A recursive function is a function which calls itself. It classic example is calculating N factorial.

N factorial is represented using N!

N factorial is calculated by taking N * (N-1) * (N-2) …. (1)

For example 5! is 5 * 4 * 3 * 2 *1.

We can calculate this with Lua like this:

Code Block
function FACTorial(N)
   if (N == 1) then
      return 1   
   end
   return N * FACTorial(N-1)
end

function main(Data)
    local Result = FACTorial(5);
end