Skip to main content

Lesson 10 · Python Functions

Module 1 · Learning to Drive

Lesson 10 · Python Functions

50–60 minPhase C · Transition to Pythondef, parameters, return
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

You built reusable functions with Blockly's "to do something" block back in Lessons 3–5. Now you'll write functions in Python with the def keyword — same idea (define once, call many times), plus the ability to pass in parameters and hand back a return value.

Learning Objectives

By the end of this lesson you will be able to:

  • Define a function in Python with def name(parameters):
  • Call a function with arguments and use the parameters inside its body
  • Return a value from a function and use it
  • Understand that variables inside a function are local to it

From "to do something" to def

Here's the reusable square, Blockly on top and Python below:

def square():
for i in range(4):
drivetrain.straight(30)
drivetrain.turn(90)

square()
square()
square()

The pieces map to what you already know:

  • def — the keyword that defines a function (like the "to do something" block).
  • square — the name (lowercase, words joined by underscores).
  • () — the parameter list (empty for now).
  • : and indentation — everything indented is the function body.
  • square()calls it. Define it once at the top; call it as often as you like.
Knowledge Check

Which keyword defines a function in Python?

Parameters make it flexible

Just like the gear icon added an input in Blockly, a name inside the parentheses adds a parameter in Python:

def square(side_length):
for i in range(4):
drivetrain.straight(side_length)
drivetrain.turn(90)

square(30)
square(50)
square(20)

The side_length parameter is a local variable; the numbers you pass (30, 50, 20) are the arguments — the same names you used in Blockly. Add more parameters by separating them with commas: def square(side_length, effort):.

Knowledge Check

Given def square(side_length):, what does calling square(45) make the robot do?

Returning a value

Some functions do something (draw a square); others calculate something and return an answer. The return keyword hands a value back to whoever called it:

def calculate_angle(sides):
return 360 / sides

angle = calculate_angle(6)
print(f"Hexagon angle: {angle}") # → Hexagon angle: 60.0

You can then build bigger functions from smaller ones — a polygon that uses calculate_angle:

def polygon(sides, side_length):
angle = calculate_angle(sides)
for i in range(sides):
drivetrain.straight(side_length)
drivetrain.turn(angle)

polygon(4, 30) # square
polygon(3, 25) # triangle
polygon(8, 15) # octagon
Knowledge Check

A variable created inside a function (like angle above) can be used…

Knowledge Check

What's wrong with this? def multiply(x, y)

Activity · Build a shape library

  1. Write polygon(sides, side_length) and test it on three shapes — the same function you built in Blockly in Lesson 5, now in Python.
  2. Add a helper calculate_perimeter(sides, side_length) that returns sides * side_length, and print the perimeter after drawing.
  3. Challenge: give your function a default value — def square(side_length=30): — so square() just works.

Real-world connections

Functions with parameters and return values are the building blocks of every program:

Everywhere

Standard libraries

Languages ship thousands of ready-made functions — you call them without knowing the internals.

Teams

Shared code

Engineers split a big system into functions so many people can work on it at once.

Robotics

Motion & perception

"read distance", "drive to", "plan path" — real robot code is functions calling functions.

Wrap-up

  • What keyword defines a function, and what punctuation must end the line? (def, and a colon.)
  • What's the difference between a parameter and an argument? (Parameter = name in the def; argument = value in the call.)
  • What does return do? (Sends a value back to whoever called the function.)

Resources