v3 · Activity 4 of 12 · functions · parameters · 9 min Runs here
Parameters are input names in a function definition. Arguments are values supplied in a call. reward(3, 4) gives completed the value 3 and perObjective the value 4.
* multiplies numbers. Return the calculation inside the function, then return the function at the end of the file so the checker can call it with different inputs.
Calculate completed * perObjective. Return the function so both zero objectives and larger jobs can be tested. Keep the final return shown in the starter so the checker can call or inspect your result.
Interface: reward(completed, perObjective) -> number
| Input | Expected |
|---|---|
Three objectives3, 4 | 12 |
No objectives0, 4 | 0 |
Different rate2, 7 | 14 |
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.