ManoCity

Module 05

Teach ManoNew Skills

Functions let us give a group of instructions a name. Once Mano learns a skill, we can ask her to use it whenever we need it.

defFunction CallsReusable CodeParameters

Watch

Meet Python Functions

Watch the short lesson, then use the interactive activity below to follow Python as it calls a function.

The Big Idea

Write once. Use many times.

Imagine Mano needs to move forward many times. Instead of repeatedly writing the same instructions, we can package them into one function.

1

Define

Use def to teach Python a new function.

2

Name

Give the skill a meaningful name such as move_forward.

3

Call

Use move_forward() whenever Mano needs that skill.

Interactive Learning

Follow the function

Step through the program and watch how Python moves from the main program into the function and then returns.

mano_functions.py
1def move_forward():
2 print("Moving forward")
3
4move_forward()

Output

Waiting for Mano...
Step 1 of 5

Teach Mano a new skill

The def keyword creates a function. We are teaching Mano a skill called move_forward.

def move_forward():

Give Functions Information

Functions can useparameters.

Mano does not always need to move the same distance. A parameter lets us give the function information when we call it.

def move_forward(distance):
print("Moving", distance,"cm")
move_forward(20)
move_forward(50)

Your Mission

Teach Mano to turn left.

Create a function called turn_left() that prints "Turning left". Then call the function twice.

def __________________():
print("__________________")

__________________()
__________________()

You've completed Module 05

Mano has learned how functions work.

Back to Python Essentials →