v3 · Activity 5 of 12 · return-values · early-return · 9 min Runs here
print exposes a value to a person; return gives it to the caller. A function without a returned value supplies nil.
return also stops the current function call. A guard at the top can reject a bad input before the main calculation.
price(quantity, unitPrice) returns 0 for a negative quantity; otherwise it returns quantity * unitPrice. The caller needs the number, not only Output. Keep the final return shown in the starter so the checker can call or inspect your result.
Interface: price(quantity, unitPrice) -> number
| Input | Expected |
|---|---|
Normal purchase3, 6 | 18 |
Empty purchase0, 6 | 0 |
Invalid count-1, 6 | 0 |
local function points(count)
if count < 0 then return 0 end
return count * 5
end
local total = points(3) + 2
print(total)
return pointspoints returns 15, allowing the caller to add 2. Merely printing 15 would not supply that value to +.
New syntax: early return, negative numbers
print exposes a value to a person; return gives it to the caller. A function without a returned value supplies nil.
return also stops the current function call. A guard at the top can reject a bad input before the main calculation.
Our rule treats a negative quantity as invalid and returns 0. This is an exercise policy, not a complete store security design.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
pricequantity, unitPrice
quantity 0
0
quantity unitPrice
priceThis is a private experiment copy. It does not replace your answer or complete the activity.
These inputs apply when your scratchpad returns the function described by the activity interface.
Your output will appear here.
Replace this code with the starter? Download your draft to keep a copy.
Your output will appear here.
Sign in with RoCreators to save private progress across devices. Community verification is separate.