Recursion

Functions

-- The function calls itself by name;
-- the first branch stops the recursion
local function fibonacci(x)
  if x <= 1 then
    return x
  end
  return fibonacci(x - 1) + fibonacci(x - 2)
end

local f10 = fibonacci(10)
-- f10 = 55

local function factorial(n)
  if n <= 1 then
    return 1
  end
  return n * factorial(n - 1)
end

local f5 = factorial(5)
-- f5 = 120

print("f10 = " .. f10)
print("f5 = " .. f5)