Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 9 of 10Free to read

New commands: def · Robot Quest

How do you teach the computer a new command?

Course contents

Why 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()
  • def means 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

Lines 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()
Step by step
  1. L1Python learns the new command. Nothing moves yet.
  2. L5The program really starts here: one move east.
  3. L6pick()... but the gem is one more square away! This example has a bug: add a move() before pick().
  4. L7(After the fix) the call jumps into the function...
  5. L2...runs both left turns...
  6. L8...then comes back and walks west to the flag.

A step command

Run it in the lesson

The loop now reads like the task: climb, three times.

def climb():
    move()
    turn_left()
    move()
    turn_right()

for i in range(3):
    climb()
Step by step
  1. L1Define climb(): four commands under one name.
  2. L8Call climb() in each round of the loop.
  3. 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():?

  1. AIt runs the two turns immediately
  2. BIt remembers the commands under that name for later
  3. CIt reports an error
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()
  1. AOn the flag
  2. BOne square from the start
  3. CThree squares from the start
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.

  1. PredictHop twice

    Predict where Bit ends up.

  2. ModifyStairs going down

    climb() goes up stairs. This staircase goes down. Change only the body of the function.

  3. 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() and collect_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

TermChineseDefinition
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.