Students meet functions that give back a value. BarWidth(stat) replaces the same width calculation written three times, then Overall(power, speed, defence) gives back one score shown for the clicked creature. Nib's Overall works the answer out into a variable and never gives it back. Make it yours adds Rank(score), with an If inside the function.
Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Try 3 min · Spot 4 min · Build 1 12 min · Trace 4 min · Build 2 10 min · Check 6 min · Make it yours 6 min
This lesson grows the Creature stats app and comes after Tidy up the journal, which introduced functions that do something. It's Level 4 (Functions); each step opens App Builder at Level 4. The starting app is the finished creature stats viewer with an empty OverallText added at the bottom.
BarWidth is a refactor (the app looks the same afterwards), so the run in step 6 of Build 1 is the check, as in the journal lesson. Overall is new behaviour.
The call fills parameters in order: Overall(Creature.power, Creature.speed, Creature.defence) puts the first value into power and so on. Swapping them doesn't change an average, which is worth noticing: for most functions order matters (try a function Difference(a, b) giving back a - b).
Scores: Blaze 57, Tide 60, Zip 55.
On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.
Each is a calculation used in many places. Written once as a function that gives back a value, it's asked for by name wherever the answer is needed.
Everyone works out Overall(80, 60, 30) on paper, then clicks Blaze to check.
170 / 3 = 56.67, rounded to 57. The click gives the same answer because the function does the same steps.
Students mix up the two kinds of function. Pick (last lesson) does something and is used with Call; BarWidth gives back a value and is used inside a value box. If a student tries Call BarWidth, ask: where would its answer go?
| What you will see | A question to ask | The fix |
|---|---|---|
| The problem says BarWidth doesn't give a value back yet. | Is there a Set return value inside BarWidth? | Click + Give back a value in the function, and fill in stat / 100 * 200. |
| All three bars are the same width. | What's inside the brackets of each BarWidth? | Each bar passes its own stat: BarWidth(Creature.power), BarWidth(Creature.speed), BarWidth(Creature.defence). |
| Overall shows a decimal like 56.666. | Which built-in function rounds a number? | Wrap it: round((power + speed + defence) / 3). |
One possible solution for each task. Other correct programs are fine.
{"version":1,"name":"Creature stats (scores)","features":{"level":4},"variables":[],"screens":[{"id":"main","name":"Creatures","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"t0","x":229,"y":20,"w":342,"h":34,"text":"Click a creature","visible":true,"vars":{}}]},{"name":"Creature","kind":"image","instanceVars":[{"name":"name","value":""},{"name":"power","value":0},{"name":"speed","value":0},{"name":"defence","value":0},{"name":"ability","value":""}],"instances":[{"id":"k0","x":235,"y":70,"w":100,"h":100,"text":"Blaze","visible":true,"vars":{"name":"Blaze","power":80,"speed":60,"defence":30,"ability":"Fire breath"},"image":"star"},{"id":"k1","x":350,"y":70,"w":100,"h":100,"text":"Tide","visible":true,"vars":{"name":"Tide","power":50,"speed":40,"defence":90,"ability":"Water shield"},"image":"water"},{"id":"k2","x":465,"y":70,"w":100,"h":100,"text":"Zip","visible":true,"vars":{"name":"Zip","power":30,"speed":95,"defence":40,"ability":"Super dash"},"image":"music"}]},{"name":"NameText","kind":"text","instanceVars":[],"instances":[{"id":"k10","x":229,"y":200,"w":342,"h":34,"text":"","visible":true,"vars":{}}]},{"name":"PowerText","kind":"text","instanceVars":[],"instances":[{"id":"k11","x":229,"y":246,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"SpeedText","kind":"text","instanceVars":[],"instances":[{"id":"k12","x":229,"y":282,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"DefenceText","kind":"text","instanceVars":[],"instances":[{"id":"k13","x":229,"y":318,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"PowerBar","kind":"shape","instanceVars":[],"instances":[{"id":"PowerBar","x":355,"y":250,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#e5484d"}]},{"name":"SpeedBar","kind":"shape","instanceVars":[],"instances":[{"id":"SpeedBar","x":355,"y":286,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#2f8fdd"}]},{"name":"DefenceBar","kind":"shape","instanceVars":[],"instances":[{"id":"DefenceBar","x":355,"y":322,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#2fa36b"}]},{"name":"AbilityText","kind":"text","instanceVars":[],"instances":[{"id":"k14","x":229,"y":362,"w":342,"h":30,"text":"","visible":true,"vars":{}}]},{"name":"OverallText","kind":"text","instanceVars":[],"instances":[{"id":"ov1","x":229,"y":400,"w":342,"h":30,"text":"","visible":true,"vars":{}}]}],"events":[{"id":"k","else":false,"conds":[{"type":"onClicked","target":"Creature"}],"actions":[{"type":"setText","target":"NameText","value":"Creature.name"},{"type":"setText","target":"PowerText","value":"\"Power: \" & Creature.power"},{"type":"setText","target":"SpeedText","value":"\"Speed: \" & Creature.speed"},{"type":"setText","target":"DefenceText","value":"\"Defence: \" & Creature.defence"},{"type":"setText","target":"AbilityText","value":"\"Special: \" & Creature.ability"},{"type":"setSize","target":"PowerBar","prop":"w","value":"BarWidth(Creature.power)"},{"type":"setSize","target":"SpeedBar","prop":"w","value":"BarWidth(Creature.speed)"},{"type":"setSize","target":"DefenceBar","prop":"w","value":"BarWidth(Creature.defence)"},{"type":"setText","target":"OverallText","value":"\"Overall: \" & Overall(Creature.power, Creature.speed, Creature.defence) & \" (\" & Rank(Overall(Creature.power, Creature.speed, Creature.defence)) & \")\""}],"children":[]}]}],"device":"web","functions":[{"id":"fbar","name":"BarWidth","params":["stat"],"events":[{"id":"fbare","else":false,"conds":[],"actions":[{"type":"setReturn","value":"stat / 100 * 200"}],"children":[]}]},{"id":"fall","name":"Overall","params":["power","speed","defence"],"events":[{"id":"falle","else":false,"conds":[],"actions":[{"type":"setReturn","value":"round((power + speed + defence) / 3)"}],"children":[]}]},{"id":"frank","name":"Rank","params":["score"],"events":[{"id":"fr1","else":false,"conds":[{"type":"compareValues","left":"score","op":"≥","right":"60"}],"actions":[{"type":"setReturn","value":"\"Strong\""}],"children":[]},{"id":"fr2","else":true,"conds":[],"actions":[{"type":"setReturn","value":"\"Still training\""}],"children":[]}]}]}
Stretch: A function Stars(score) that gives back a row of stars: "★" for a score under 50, "★★" under 70, "★★★" otherwise (If, Else if, Else inside the function). Which is easier to read: Stars(Overall(…)) in the text, or the Ifs written out in the click event?
Support: Do Build 1 together for PowerBar on the board; pairs change the other two bars and do Build 2.
A canteen app works out price × quantity for every item on an order. What function would you make, what would it give back, and how would you use it?
Total(price, qty) gives back price * qty. Each item's line uses Total(item price, item quantity) in a value box, and the order total adds them up.
Next: the challenge, a prototype of their own. A function that gives back a value is worth reaching for whenever the same calculation appears twice.