Transfer practice · Calculate how many supplies remain: remaining(stock, people, eachUse). First multiply people * eachUse to find the amount used. Then subtract that amount from stock: stock - used. The - sign means subtraction. Multiplication is calculated before subtraction in a one-line expression; storing used on a separate line makes the order clear. Return the remaining number and keep the final return remaining.
v3 · Activity 1 of 1 · functions · parameters · 9 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
Calculate how many supplies remain: remaining(stock, people, eachUse). First multiply people * eachUse to find the amount used. Then subtract that amount from stock: stock - used. The - sign means subtraction. Multiplication is calculated before subtraction in a one-line expression; storing used on a separate line makes the order clear. Return the remaining number and keep the final return remaining.
Interface: reward(completed, perObjective) -> number
| Input | Expected |
|---|---|
Three builders20, 3, 4 | 8 |
No builders20, 0, 4 | 20 |
local function totalCost(quantity, unitPrice)
return quantity * unitPrice
end
print(totalCost(2, 6))
return totalCostThe call prints 12. The returned function can calculate another total without changing its definition.
New syntax: parameters, arguments, *, - (optional practice), multiplication before subtraction (optional practice)
A function names reusable behavior. Parameters are the local input names in the definition. Arguments are the actual values supplied when it is called.
The order of arguments matches the order of parameters. reward(3, 4) gives completed the value 3 and perObjective the value 4.
Defining a function does not run its body. This activity returns the function so tests can call it repeatedly with different inputs.
In the optional practice, - subtracts an amount. Multiply people * eachUse to find the total used, then subtract it from stock. You can store that intermediate result in a local used variable. In stock - people * eachUse, multiplication happens before subtraction; stock - (people * eachUse) makes the same order explicit.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
rewardcompleted, perObjective
completed perObjective
rewardThis 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.