← Back to course
Code Crew G4 / Week 10 / Crew Quest
6/6
Week 10 Β· Functions that return

Crew Quest

Crew Quest: Function Machine
// Name it, feed it, get an answer
⏱ about 15 min

Crew Quest: Function Machine

Rocket builds a function machine out of a cardboard box. Input cards go in one slot. Answers come out the other.

Raven drops in a card that says 6. Rocket, hiding inside, works it out and pushes back a card that says 12. "What do you notice?" Raven asks the crew. "What is the machine doing?"

Nova guesses first. "Times two! That is a return value," she says with a happy flicker.

  1. def gives a group of lines a name. Calling the name runs them.
  2. Parameters receive arguments, in order.
  3. return hands a value back. print only shows it.
  4. A function with no return gives back None.
  5. Functions can call other functions.
β—‡ FUNCTION MACHINE
One person is the machine. Pick a secret rule, like add 3.
The other person hands in number cards, one at a time.
The machine hands back the answer card. That is the return value.
Guess the rule. Write it as a Python def with return.
Swap jobs and pick a new rule.

Here is a different machine. Its rule is times 2, then add 1.

def machine(n):
    return n * 2 + 1

print(machine(6))
print(machine(machine(1)))
First line on the screen
Second line on the screen
BONUS RECAP
  • Read the question.
  • Tap your answer.
f(x) returns x + 1. What is f(4)?
What does a function with no return give back?
In the call f(7), what is 7?

Great building! Next week, you will tidy up a long program, like a real engineer.

← Friday