New commands: def · Robot Quest
How do you teach the computer a new command?
Course contentsWhy you need it
Bit only knows a few commands, so programs fill up with the same little groups: "turn left, turn left" to turn around, four lines to climb a step. A function gives a group of lines a name. Then the name works like a new command, and your program reads like a description of the task.
The intuition
A recipe says "make the sauce (see page 12)". You learn "make the sauce" once, then use it in many recipes.
def turn_around():
turn_left()
turn_left()defmeans define: "here is a new command called turn_around".- The indented lines are what it does.
- Defining it does not run it. It runs only when you call it:
turn_around().
When Python reaches turn_around(), it jumps into the function, runs its lines, then comes back to the line after the call.
Definition
Function
A function is a named block of code that runs when it is called; def defines it.
Call
To call a function is to run it by writing its name followed by parentheses.
Examples
Turn around
Run it in the lessonLines 1–3 only define the command. Watch the highlight jump into the function at line 7.
def turn_around():
turn_left()
turn_left()
move()
pick()
turn_around()
for i in range(4):
move()- L1Python learns the new command. Nothing moves yet.
- L5The program really starts here: one move east.
- L6pick()... but the gem is one more square away! This example has a bug: add a move() before pick().
- L7(After the fix) the call jumps into the function...
- L2...runs both left turns...
- L8...then comes back and walks west to the flag.
A step command
Run it in the lessonThe loop now reads like the task: climb, three times.
def climb():
move()
turn_left()
move()
turn_right()
for i in range(3):
climb()- L1Define climb(): four commands under one name.
- L8Call climb() in each round of the loop.
- L2Inside the function: the four commands run, then Python returns to the loop.
Common mistakes
Defining is not running
If you only write def ... and never call it, nothing happens. Remember the call: climb().
Define before you call
Python reads top to bottom. Calling climb() above its def is an error, because the name doesn't exist yet.
Quick quiz
1. What happens when Python reads the line def turn_around():?
Show the answer
Answer: B
def only defines. The body runs each time the function is called.
2. Where does Bit end up?
def go():
move()
move()
move()Show the answer
Answer: B
Defining go does not run it. Only the last line runs: one move.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictHop twice
Predict where Bit ends up.
- ModifyStairs going down
climb()goes up stairs. This staircase goes down. Change only the body of the function. - WriteThere and back
Bit starts in the middle of a corridor. Collect the gems to the east, then turn around and collect the ones to the west; the flag is at the west end. Define
turn_around()andcollect_to_wall(), and make it work on all 3 maps.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Function | 函数 | A named block of code that runs when it is called. |
| Define | 定义 | To create a function with def, giving its name and body. |
| Call | 调用 | To run a function by writing its name followed by parentheses. |