← App Builder apps

Creature scores

Functions that give back a value: BarWidth(stat) works out a bar's width once, for all three bars, and Overall(power, speed, defence) gives back one score for each creature. Find out what happens when a function works something out but never gives it back.

A shop works out a price with tax, a game works out damage, a fitness app works out a score. Each is one calculation used in many places, written once and asked for by name.

In Tidy up the journal, a function did something: Pick set Chosen and went to Describe. A function can also work something out and give it back, like a calculator button:

You use it wherever a value goes, in a value box, just like count(Journal) or round(2.6). Today's app is the creature stats viewer, tidied and given an overall score.

How this lesson works: See it, Check, Build, Make it yours
See itTry the finished thing first, so you know what you are building.

Try the finished app

Click each creature. The bars and the Overall score at the bottom change. Which creature has the highest score?

Try it yourselfYour edits are saved in this browser.

CheckA quick check that the idea has stuck.

Spot the repetition

Look at the last three actions of Creature is clicked: Set PowerBar width to … and the two after it.

Open the Events tab. The three bar actions each set a width. What is different between them?

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: BarWidth gives back a width

  1. In the sidebar, under Functions, click + Function and name it BarWidth.
  2. + Parameter: stat.
  3. Click + Give back a value. It adds Set return value to: fill in stat / 100 * 200
  4. Click This screen: Creatures. In Creature is clicked, click Set PowerBar width to … and change the value to BarWidth(Creature.power)
  5. Do the same for SpeedBar (BarWidth(Creature.speed)) and DefenceBar (BarWidth(Creature.defence)).
  6. Run it and click each creature. The bars should look exactly as before.

Want wider bars? Change 200 in one place, BarWidth, and all three follow.

A function that gives back a value · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

A function BarWidth(stat) gives back stat / 100 * 200, and all three bars use it.

    Run your program. This ticks when your code does all of the above.

    CheckA quick check that the idea has stuck.

    What does it give back?

    Zip's speed is 95. What does BarWidth(Creature.speed) give back when Zip is clicked?

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: Overall gives back a score

    1. + Function: Overall, with three parameters: power, speed, defence (in that order).
    2. + Give back a value: round((power + speed + defence) / 3) (the average, rounded to a whole number).
    3. In Creature is clicked, add Set OverallText text to "Overall: " & Overall(Creature.power, Creature.speed, Creature.defence)
    4. Run it. Which creature scores highest?

    The call fills the parameters in order: the first value goes into power, the second into speed, the third into defence.

    Try it yourselfYour edits are saved in this browser.

    A function Overall(power, speed, defence) gives back the rounded average, and OverallText shows it for the clicked creature.

      Run your program. This ticks when your code does all of the above.

      CheckA quick check that the idea has stuck.

      Check: Nib's Overall

      Nib's Overall does the maths, but the app says it doesn't give a value back.

      Work it out before you click Blaze.

      Clicking a creature shows the problem "Overall doesn't give a value back yet". Click the line where Nib goes wrong.

      Blaze has power 80, speed 60 and defence 30. What does Overall give back?

      Make it yoursChange your app on purpose, then try it on someone.

      Make it yours: a rank

      Make a function Rank(score) that gives back a word for a score, such as Strong for 60 or more and Still training below that:

      1. + Function Rank, parameter score.
      2. In its event, a condition: Compare score ≥ 60, then Set return value to "Strong".
      3. From the event's ⋯ menu, Add an Else below, with Set return value to "Still training".
      4. Show it beside the score: add & " (" & Rank(Overall(Creature.power, Creature.speed, Creature.defence)) & ")" to the end of the OverallText value.

      Rank(Overall(…)) asks Overall first, then hands its answer straight to Rank.

      Try it yourselfYour edits are saved in this browser.

      A function Rank(score) gives back one word when the score is high and another otherwise (an If and an Else), and the creature's rank is shown.

        Run your program. This ticks when your code does all of the above.

        Now you can
        Next lesson12. Challenge: your own appPlan, build and test a prototype of an app for a real person. Choose a problem, sketch the screens and how they connect, decide which variables and events it needs, build it in milestones, then watch someone use it and improve one thing.
        For teachers

        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. About 45 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        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.

        Running the lesson

        1. Spot the repetition: same as the journal lesson. Circle what's the same, underline what's different.
        2. Build 1: checkpoint: do the bars look exactly as before? That's the test for a refactor.
        3. Trace: work out Zip's speed bar by hand before anyone clicks.
        4. Check: vote on Blaze's score, then click.

        The hard part (on purpose)

        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?

        When students get stuck

        Stretch and support

        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.

        Where it leads

        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.

        What each step is for