CodeRigor

FREE LESSON

Control flow, functions & multiple results

Write local functions, validate preconditions and distinguish a function’s returned values from printed output.

Before this lesson, complete lua-values.

This course runs standard Lua 5.5 with its standard library. Game engines, LuaJIT and embedded host APIs are outside this sequence.

Lua uses then/end around an if branch, do/end around loops, and end to finish a function. Conditions follow Lua truthiness, so comparisons should express the intended numeric relationship directly. An elseif chain selects the first matching branch. For a clamp operation, values below the lower bound map to that bound, values above the upper bound map to the upper bound, and interior values survive unchanged.

Inclusive clamp
start / 3
Inclusive clamp

Apply the contract

Functions are first-class values and can capture locals. Parameters are local to a call, and assigning to one does not overwrite the caller's scalar variable. Lua also supports multiple return values. Assignment adjusts the number of results to the receiving variables; a result used as a single expression is adjusted to one value. Parentheses around a call can force that single-result behavior, so inspect call position before assuming that every result propagates.

A function call boundary
start / 3
A function call boundary
Lua
local function divide(a,b)
  if b == 0 then return nil, "zero divisor" end
  return a/b
end
local value, err = divide(8,2)
print(value, err)
print(divide(8,0))

Reason about the edge cases

Document the valid input domain. The lab supplies finite numbers and requires ordered numeric bounds; assert is suitable for rejecting a violated programming precondition. An ordinary out-of-range value is expected input and should be clamped rather than treated as an error. pcall converts a raised error into a false status and an error value so a caller can inspect it. Test both exact endpoints, an interior value and reversed bounds. Avoid inventing a tolerance or rounding rule: the function returns an existing numeric value or one of the bounds.

Precondition versus ordinary input
start / 3
Precondition versus ordinary input

Practice

Implement M.clamp(value, low, high) for finite numbers: return low below the lower bound, high above the upper bound, and value otherwise. Bounds are inclusive and may be equal. Raise an error for invalid argument types or reversed bounds. Do not print; return the result to the caller.

Run sample starts again from the first public fixture and grants no mastery evidence. Submit checks all fixtures, including edge cases. Read the specification before editing; the bug and fill phases share the build’s contract. Flashcards reinforce recall on a separate schedule.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

The first part introduces Lua through its language designer. The authored lesson and Lua 5.5 manual cover the exact APIs and advanced details in this lab.