Functions and loops, lesson 12
Parameters and return values
Goal: Give a function inputs and get a result back.
about 6 min3 practice exercises
Functions get more useful when you can give them values and get a result back.
Parameters are the inputs, listed in the brackets. return sends a result back to whoever called the function.
local function awardCoins(current, amount)
return current + amount
end
local coins = 0
coins = awardCoins(coins, 5)
coins = awardCoins(coins, 5)
print(coins) -- 10
When line 6 runs, current is 0 and amount is 5 inside the function, so it returns 5, which is stored back in coins.
A function can return a boolean too, which reads nicely in an if:
local function canOpenGate(coins, required)
return coins >= required
end
if canOpenGate(12, 10) then
print("The gate opens")
end
return also ends the function immediately. Any line after it in the same block does not run.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.