Skip to content
Exit presentation

Computational Thinking III: Functions & Debugging

One call inside another

One cloth bag holds the morning fares. Another holds the evening fares. One line finds which bag has the bigger total.

max(total(morning), total(evening))
Step 1 of 4
  1. max(total(morning), total(evening))
  2. total(morning) finishes first: 30.
  3. total(evening) finishes next: 34.
  4. max picks the larger: 34.

Press the button below. Watch each call turn into a number.

Loading lab…

The rule

The list morning holds 8, 6, 10 and 6 rupees. The list evening holds 12, 8, 6 and 8.

total adds up a bag and hands back the sum. max takes two numbers and hands back the larger.

A call is a value. So a call can sit inside another line. The inside call runs first. It hands its value up. Only then does the outer line have what it needs.

`` max(total(morning), total(evening)) ``

  1. total(morning) runs all the way through. It gives 8 + 6 + 10 + 6 = 30.
  2. total(evening) runs. It gives 12 + 8 + 6 + 8 = 34.
  3. max(30, 34) runs. It gives 34.

So the evening bag has the bigger total, 34.

Jump to lesson

This track

  1. 0 Start here
  2. 1 A name for a group of steps
  3. 2 What you hand it, and what each end is called
  4. 3 The value it hands back
  5. 4 One call inside another
  6. 5 A print in the middle
  7. 6 One line at a time
  8. 7 Predict, then run
  9. 8 Halve the hunt
  10. 9 The big picture
  11. 10 Track quiz

Other tracks