Module 6 · Functions and scope
Why functions exist
Define reusable named behavior and call it when needed.
What you will learn
- Distinguish defining from calling a function
- Use a local function to avoid repeated behavior
- function
- A value containing behavior that runs when called.
- call
- Ask a function to run by using parentheses.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest may show the same round message in several places. A function gives that behavior one name and one implementation.
The mental model
Defining a function describes behavior and stores it under a name. Calling the function runs that behavior.
A definition does not run its body immediately. Each call creates a new execution of the body.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
end is read as “end”: it closes a block such as a decision, loop, or function.
A tiny example
--!strictlocal function announceRound() print("A new round begins")endannounceRound()announceRound()Line by line
- 3
local function defines reusable behavior named announceRound.
- 4
The indented statement is its body.
- 5
end closes the definition without calling it.
- 7
Parentheses call the function once.
- 8
A second call repeats the same behavior.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: local function defines reusable behavior named announceRound.
Line 4: The indented statement is its body.
Line 5: end closes the definition without calling it.
Line 7: Parentheses call the function once.
Line 8: A second call repeats the same behavior.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: Each explicit call runs one copy of the named behavior.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
How many messages appear?
Try it
Mark each step after you have tried it.
Common mistake
announceRoundThe name alone refers to the function value. Add parentheses when you intend to call it.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Definitions create behavior.
- Calls run that behavior.
- A good name states what the function does.
Next: Next, you will give functions inputs through parameters.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.