Back

Activity 2.5: Writing Your Own Functions

divider

Activity 2.5

Writing Your Own Functions

Key Concepts

Give a Group of Lines a Name

Define It, Then Call It

Parameters Fill in the Blanks

Yesterday's Code Repeats Itself

2-4-launch-countdown, the end
for i in range(5):
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.2)
led1.value(1)
led2.value(1)
led3.value(1)
time.sleep(0.2)
led1.value(0)
led2.value(0)
led3.value(0)

"All off" is written out twice. Change how it works, and you have to change it everywhere.

Give It a Name

A function is a group of lines with a name. def defines it.

def all_off():
led1.value(0)
led2.value(0)
led3.value(0)

Run this. What do the LEDs do?

Give It a Name

def all_off():
led1.value(0)
led2.value(0)
led3.value(0)
all_off()

Nothing, until the last line. Defining a function only teaches Python the name.

Calling it — the name with () — runs the lines.

A Parameter Fills in the Blank

A parameter is a name inside the function. Each call fills it in.

def light(led):
led.value(1)
time.sleep(1)
light(led1)
light(led2)
light(led3)

A Parameter Fills in the Blank

def light(led):
led.value(1)
time.sleep(1)
light(led1)
light(led2)
light(led3)
light(led1): red lit
light(led2): red and yellow lit
light(led3): all three lit

One definition, three calls: led is a different LED each time.

Same Function, Different Launch

def flash(times, pause):
for i in range(times):
all_off()
time.sleep(pause)
all_on()
time.sleep(pause)
all_off()

Two parameters: how many flashes, and how long each pause is.

Same Function, Different Launch

flash(2, 0.5): two half-second flashes over 2 secondsflash(5, 0.2): five fast flashes, also over 2 seconds

The values you pass in are the arguments. Change them and the same lines do something new.

Not Shorter. Better.

Today's program ends up longer than yesterday's.

But "all on" now lives in one place. Change it once and every flash changes.

That's the point of a function: one idea, one place.

Today's Objectives

  • Defining a function with def
  • Using a parameter to make a function work with different values
  • Replacing repeated code with function calls

Key Terms

Function
A named group of lines that runs when it is called.
def
The keyword that defines a function.
Call
Running a function by writing its name with parentheses.
Parameter
A name in the function's definition that each call fills in.
Argument
The value a call passes in for a parameter.
Refactor
Rewriting code without changing what it does.

'F' → Fullscreen

divider

Build

No new wiring: today uses Activity 2.4's three LEDs exactly as they are. Open 2-4-launch-countdown and save a copy as 2-5-mission-control. Work in the copy.

Every code block is your whole program, from line 1. The green + lines are what you add. Don't type the + sign.

Today, tasks also take lines out. Each one says which lines go and how long your program should be afterward.


Task 1: Name the Repeats

  • Add the two definitions, all_off() and all_on(), after the three Pin lines.
  • Replace the six value() lines inside the loop with two calls, and the last three lines with one call. Your program goes from 27 lines to 31.
  • Run it. The LEDs must do exactly what yesterday's did — the same countdown, the same five flashes. If anything is different, the refactor has a mistake.
2-5-mission-control
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def all_off():
led1.value(0)
led2.value(0)
led3.value(0)
def all_on():
led1.value(1)
led2.value(1)
led3.value(1)
led1.value(1)
time.sleep(1)
led2.value(1)
time.sleep(1)
led3.value(1)
time.sleep(1)
for i in range(5):
all_off()
time.sleep(0.2)
all_on()
time.sleep(0.2)
all_off()

Task 2: A Parameter

  • Add light(led) after all_on().
  • Replace the six countdown lines (three value(1), three sleep(1)) with three calls. 31 lines to 32.
  • Run it. Still exactly the same as yesterday.
2-5-mission-control
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def all_off():
led1.value(0)
led2.value(0)
led3.value(0)
def all_on():
led1.value(1)
led2.value(1)
led3.value(1)
def light(led):
led.value(1)
time.sleep(1)
light(led1)
light(led2)
light(led3)
for i in range(5):
all_off()
time.sleep(0.2)
all_on()
time.sleep(0.2)
all_off()

Task 3: Two Parameters

  • Add flash(times, pause). Its loop is the one from Task 1.
  • Replace the loop and the all_off() after it with one call, flash(5, 0.2). Then add a slow systems check, flash(2, 0.5), before the countdown. 32 lines to 36.
  • Before you run it, predict how long the systems check takes, and how long liftoff takes. Then time them.
2-5-mission-control
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def all_off():
led1.value(0)
led2.value(0)
led3.value(0)
def all_on():
led1.value(1)
led2.value(1)
led3.value(1)
def light(led):
led.value(1)
time.sleep(1)
def flash(times, pause):
for i in range(times):
all_off()
time.sleep(pause)
all_on()
time.sleep(pause)
all_off()
flash(2, 0.5)
light(led1)
light(led2)
light(led3)
flash(5, 0.2)

Task 4: Break It on Purpose

Four changes, one at a time. Record what happens, and undo each one before the next.

  1. Delete the last line, flash(5, 0.2). Run it. Is there an error? What do the LEDs end up doing?
  2. Move flash(2, 0.5) up so it sits above def all_off():. Run it, and copy the error message exactly.
  3. Change flash(2, 0.5) to flash(). Run it, and copy the error message exactly.
  4. Delete the led2.value(1) line inside all_on(). Run it. Which parts of the show changed, and which didn't?

Two of these are errors and two aren't. For each, say why Python did or didn't complain.

divider

Checkpoint

  • After Tasks 1 and 2, your LEDs did exactly what Activity 2.4's did
  • Your final program runs a slow systems check, the countdown, then a fast liftoff
  • Your Task 3 predictions are written next to your timings
  • All four Task 4 results are recorded, with both error messages copied exactly
divider

Reflection

Answer the following questions before submitting your work.

  1. Your program got longer today, not shorter. Explain what using functions gained anyway, using Task 4's change to all_on() as your example.
  2. In def light(led):, led is a parameter. Explain how it can be a different LED on each call.
  3. Deleting the call to flash(5, 0.2) caused no error, but moving a call above its definition did. Explain the difference.
divider

Submit

Submit your 2-5-mission-control program file and your completed Activity 2.5 sheet.

Activity Complete