Read a return value
On this page
LESSON 21
A return value is an answer sent back to the code that asked a question. It can be a word, a number, true, false or another Lua value.
local Script = {}local function can_pay(coins, price)
return coins >= price
endfunction Script.run(context)
local allowed = can_pay(4, 3)
Log(tostring(allowed))
return allowed
endreturn ScriptFollow the call#
Lua passes 4 as coins and 3 as price. The comparison succeeds, so can_pay returns true. The caller stores that answer in allowed. Log turns the value into readable output. The next line returns the same answer to whoever started run.
What happens at the boundary?#
| can_pay(3, 3) | Returns true. The >= comparison includes an exact match. |
|---|---|
| can_pay(2, 3) | Returns false. The function ran successfully and answered the question. |
| No explicit return | An ordinary function with no returned values yields nil when its caller assigns the result to a single local. |
For a function in another PCA record, CallScript accepts the record ID and function name. Programming page 19 shows how to capture that answer across records.
