Module 6 · Functions and scope
Returning results
Produce a value in a function and use it at the call site.
What you will learn
- Return a value from a function
- Store and use the returned result
- return value
- A value sent from a function back to its caller.
- call site
- The location where a function is called.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest calculations are more reusable when a function returns a score instead of deciding how every caller displays it.
The mental model
return ends the current function call and sends a value back. The call expression becomes that value.
The caller can store it, compare it, format it, or pass it elsewhere.
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.
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.
return is read as “return”: it ends the current function call and sends a value to its caller.
A colon in a declaration, such as coins: number, introduces a type annotation: the kind of value that name is allowed to hold.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal function scoreCoins(coins: number): number return coins * 10endlocal score = scoreCoins(6)print(score)Line by line
- 3
The annotation after parentheses promises a numeric result.
- 4
return sends the calculated number to the caller.
- 7
The call expression produces a value stored in score.
- 8
The caller chooses to print it.
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: The annotation after parentheses promises a numeric result.
Line 4: return sends the calculated number to the caller.
Line 7: The call expression produces a value stored in score.
Line 8: The caller chooses to print it.
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: The function calculates while the caller decides how to use the result.
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
Which value does scoreCoins(6) produce?
Try it
Mark each step after you have tried it.
Common mistake
local function scoreCoins(coins: number): number print(coins * 10)endPrinting a calculation does not return it. A caller that needs the number must receive it with return.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- return sends a result to the caller.
- A call expression can become a stored value.
- Calculation and presentation can stay separate.
Next: Next, you will see where local names exist and why boundaries matter.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.