Skip to content
Exit presentation

Computational Thinking III: Functions & Debugging

The value it hands back

Printing an answer and returning it look the same on screen. Returning hands the answer back to the line that called the function.

ticketFare(3). The 3 goes in.
Step 1 of 4
  1. ticketFare(3). The 3 goes in.
  2. The body works out 8.
  3. return sends 8 back out.
  4. A print-only body hands back nothing.

Slide the stops below. Compare the two boxes called due.

Loading lab…

The rule

`` function ticketFare(stops) { let full = 2 + 2 * stops; if (full > 12) { return 12; } return full; } ``

The if line limits the fare to 12. For 3 stops, full is 2 + 2 × 3 = 8. That is not above 12, so the first return is skipped.

ticketFare(3) runs, reaches return full, and becomes 8.

return hands one value back to the line that called it. That line becomes the value. A function that only prints puts a line on the screen. It hands back nothing.

let due = ticketFare(3); stores 8 in due. With a print-only body, due holds nothing. That is the value null, which means no value.

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