Menu
CoddyTech

Functions Returning Nothing

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 25 of 80.

Plenty of functions don't compute a value. They do something: print a message, update a table, save state. In Luau you can annotate that a function returns nothing with the empty return type ():

local function logMessage(message: string): ()
    print(message)
end

local counter = 0
local function incrementCounter(): ()
    counter += 1
end

If you've seen TypeScript, () plays the role of void, but the Luau spelling is a pair of empty parentheses, literally "zero return values".

The annotation is optional: leave it off and Luau infers that the function returns nothing. Writing : () explicitly documents your intent: anyone reading the signature knows this function is called for its side effects, not its result.

At runtime nothing changes (as usual): a function without a return value simply produces nil if you try to capture its result: same as plain Lua.

challenge icon

Challenge

Easy

Write two functions that print and return nothing.

  1. Write local function displayWelcome(userName: string): () that prints `Welcome, {userName}!`.
  2. Write local function logError(errorMessage: string): () that prints `ERROR: {errorMessage}`.
  3. Call displayWelcome("Alice"), then logError("Invalid input").

Press Run Code: the output shows Welcome, Alice! and ERROR: Invalid input.

Try it yourself

-- 1) displayWelcome(userName: string): ()
-- 2) logError(errorMessage: string): ()
-- 3) call displayWelcome("Alice"), then logError("Invalid input")

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler