Local variables inside a function are separate from local variables outside it.

isolated names The same name can appear in different local scopes without sharing one value.

Local Scope

base
local_scope.lua
Replay: real traced execution (multi-file project)
local base = 10

local function add_fee(amount)
  local fee = 3
  return amount + fee
end

local total = add_fee(base)

print("base=" .. base)
print("total=" .. total)
local base = 4

local function add_fee(amount)
  local fee = 3
  return amount + fee
end

local total = add_fee(base)

print("base=" .. base)
print("total=" .. total)
local base = 20

local function add_fee(amount)
  local fee = 3
  return amount + fee
end

local total = add_fee(base)

print("base=" .. base)
print("total=" .. total)
  1. base ← 10

    1local base→ 10 = 10 --@base=4, 2023local function add_fee(amount)4  local fee = 35  return amount + fee6end78local total = add_fee(base10)
  2. total ← 13

    8local total→ 13 = add_fee(base10)910print("base=" .. base10)11print("total=" .. total13)
    outputbase=10
    total=13
  1. base ← 4

    1local base→ 4 = 423local function add_fee(amount)4  local fee = 35  return amount + fee6end78local total = add_fee(base4)
  2. total ← 7

    8local total→ 7 = add_fee(base4)910print("base=" .. base4)11print("total=" .. total7)
    outputbase=4
    total=7
  1. base ← 20

    1local base→ 20 = 2023local function add_fee(amount)4  local fee = 35  return amount + fee6end78local total = add_fee(base20)
  2. total ← 23

    8local total→ 23 = add_fee(base20)910print("base=" .. base20)11print("total=" .. total23)
    outputbase=20
    total=23