Skip to content
Your progress

Checking your sign-in…

Who's learning?

Each profile keeps its own read lessons, badges, streak, and practice scores on this device.

Lesson 3 of 11

The value it hands back

Show on a big screen

6 min

Text size

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.

Try the lab to finish this lesson.