← Back to course
Code Crew G6+ / Week 06 / Crew Quest
6/6
Week 06 Β· Functions and modules

Crew Quest

Crew Quest: Function cards
// Name a job once, use it everywhere
⏱ about 30 min

Saturday: Crew Quest: Function cards

Rocket has a stack of index cards on the table. On each card he writes a function name, its parameters and what it returns, like a recipe card for code.

"If I can explain a function on one small card," he says, "then I really understand it."

Raven picks up a card and reads it aloud. "What do you notice?" she asks. "The card does not show how the function works inside. Just what goes in and what comes out."

Nova arranges the cards in a neat row in the air. "Interesting," she says. "That is how programmers think about functions they did not write. Inputs, output, and a promise."

Rocket hands a blank card across the table to the nearest grown-up.

  1. def name(): defines a function. Calling name() runs it.
  2. Parameters receive arguments, in order, or by name.
  3. return hands a value back. Without it, a function returns None.
  4. Defaults fill in missing arguments. A docstring says what the function does.
  5. Any .py file is a module. import it from a program in the same folder.
β—‡ FUNCTION CARD GAME
Each of you writes three function cards: a name, its parameters and what it returns. For example: double(n) returns n times 2.
Swap cards. For each card, write a call with real arguments, and predict what it returns.
Pick your favorite card and turn it into real Python in IDLE. Test it with three different arguments.
def triple(n): return n * 3. What is triple(7)?
def hello(name="crew"): return "Hello, " + name. What does hello() return? Hello, ____
WEEK 6 CHECK
  • Read the question.
  • Tap your answer.
def add(a, b): print(a + b). What does x = add(2, 3) store in x?
Which line imports a module saved as tools.py?
MORE REVIEW: WEEK 6
  • Read the question.
  • Tap your answer.
def double(n): return n * 2. What is double(double(3))?
def hi(name="crew"): print("Hi", name). What does hi() print?
What does a function without return hand back?

Brilliant work. Next week you will explore modules that come with Python: random and datetime.

← Friday