Skip to content
Exit presentation

Computational Thinking III: Functions & Debugging

Track quiz

Eight lessons, one rule each. Keep the table open beside the quiz.

The eight rules, kept in one place
IdeaThe rule
A name for stepsA function gives a group of lines one name. Writing the definition runs nothing. It runs at each call, and what gets printed does not change.
What you hand itThe name written in the brackets of the definition is the parameter. The value written in the brackets at the call is the argument.
The value it hands backreturn 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.
One call inside anotherA 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.
A print in the middleA print in the middle shows what a box held right then. That is often not what you assumed. Printing changes nothing else about the run.
One line at a timeStepping through shows every box after every line. That is what a print on every line would show. Look first at the row that differs from what the job expects.
Predict, then runWrite down what each line should print, before you run it. Predict from the job, not from the program. The first line where they differ is the one to check.
Halve the huntSay one line is wrong. Its wrong result stays in the total. Check the middle of what is left. Drop the half that cannot hold the fault. Then check again.

A definition sits alone. No call is anywhere. What runs?

  • The body runs once, at the definition
  • Nothing from the body runs
  • The run stops with an error

Six repeated lines become one function and two calls. What changes about the output?

  • Nothing. The same lines print in order
  • The lines print once, at the definition
  • The lines print faster, as the run is shorter

In function fare(stops, rider), what is stops?

  • An argument
  • A value
  • A parameter

In fare(3, "adult");, what is 3?

  • A parameter
  • An argument
  • A function

In this language, fare(5) is called, but fare needs two values. What happens?

  • A fare is worked out from one value
  • The run carries on
  • The run stops with a message

A function body is one print(8);. What does let due = f(); put in due?

  • Nothing
  • 8
  • The word print

print("Fare: " + ticketFare(3)) works. Why?

  • `print` runs the function itself, then joins the two
  • `ticketFare(3)` returns a number before the join
  • A call always turns into text before the join happens

In max(total(a), total(b)), what runs first?

  • `max`
  • Both at once
  • `total(a)`

A wrong fare prints, and there is no message. What does a print in the middle show?

  • What a box held right then
  • Which line is wrong, by name
  • A faster run

Does adding a print change the answer of a broken program?

  • Yes. It fixes the box
  • Yes, but only in a function
  • No. It only reads a box

A trace table shows a dash, then 6 on the next row. What was the dash?

  • The box held zero
  • The box did not exist yet
  • The table could not read it

You predict by reading the code, then run it. They match. What does that prove?

  • Very little. The prediction copied the code
  • The program is right, since the two lines match
  • The prediction was made too late to be of any use

Ten printed lines are wrong. Which do you check first?

  • The last line that is wrong
  • The first wrong line
  • All ten, one after another

Sixteen lines run in order, and one is wrong. Its wrong result stays in the total. What do you check first?

  • The middle line
  • Line 1, then walk down
  • The last line, then walk up

The fault is on line 10 of 16. Halving takes 4 checks. Checking from the top takes?

  • 4 checks
  • 16 checks
  • 10 checks

Why does halving fail on a jumbled row of cards?

  • Cards are not lines of a program
  • No order, so one check drops nothing
  • A row is too short for two halves to exist

Next comes Data Handling I: Organizing & Visualizing Data. You handed a number to a function here. There you will hand it a whole list.

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