Syntax
local function add(left, right)
return left + right
endExamples
Creating a counter closure
A small, runnable example of this syntax.
local function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
local next_count = make_counter()
print(next_count()) -- 1
print(next_count()) -- 2Best practices
- Prefer local function declarations for named helpers so they do not leak into the global environment.
- Keep examples small while learning, then combine the idea with a real project.
At a glance
- Purpose
- Lightweight scripting and embedding
- File extension
- .lua
- Runs in
- Lua interpreter or a host application
- Usually used with
- Host APIs and Lua modules