🎯 What you will learn
- A command tells a robot one action
- A function groups commands with a name
- Calling a function runs it
👋 Let’s get started!
A robot can drive, turn and stop, but only when a program tells it to. Each action is a command. When we give a command a name, it becomes a function, and we can use it again and again. 🤖
🪜 Step by step
1. 🕹️ Commands
A command is one action: move forward or turn left. Robots follow each command in order.
print("Robot moves forward")
2. 🧩 Functions
A function is a group of commands with a name. Write def, then the name, then the steps.
def turn_left():
print("Robot turns left")
3. 📣 Calling
To run a function, write its name with brackets: turn_left().
turn_left()
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Robot route
def forward(steps):
print("Robot moves forward", steps, "steps")
def turn_left():
print("Robot turns left")
forward(3)
turn_left()
forward(2)🎨 Unplugged activity
🕹️ Robot Says, again! Write 5 commands on cards (forward, turn left, turn right, stop). Pick 3 cards and have a grown-up act them out exactly.
Drive to the charger!
forward(2)
turn_left()
forward(4)
turn_left()
forward(1)📖 Key words
Tap a card to flip it over!
- Command
- One action for a robot 🕹️
- Function
- A named group of commands 🧩
- Call
- Running a function by name 📣
🏆 Challenge
Make the robot move forward 5 steps, then turn left. Press ▶ Run to check.
Challenge
def forward(steps):
print("Robot moves forward", steps, "steps")
def turn_left():
print("Robot turns left")
Knowledge Check Quiz
- What is a function?
- What does turn_left() do?
- Robots follow commands in which order?