Functions Returning Nothing
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 24 of 73.
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 ():
function logMessage(message: string): ()
print(message)
end
local counter = 0
function incrementCounter(): ()
counter += 1
endIf 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
EasyCreate a function named displayWelcome that takes userName: string and has an explicit return type of (). It should print: "Welcome to our application, [userName]!"
Create a function named logError that takes errorMessage: string with return type (). It should print: "ERROR: [errorMessage]"
Create a function named processData that takes no parameters, return type (). It should print "Processing data..." and then "Data processing complete." on the next line.
Call them in this order:
displayWelcome("Alice")processData()logError("Invalid input detected")displayWelcome("Bob")
Try it yourself
-- Write code here
-- displayWelcome(userName: string): ()
-- logError(errorMessage: string): ()
-- processData(): ()
-- then call: displayWelcome("Alice"), processData(),
-- logError("Invalid input detected"), displayWelcome("Bob")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps