Back

Activity 2.5: Writing Your Own Functions

divider

Activity 2.5

Writing Your Own Functions

Key Concepts

Functions

def

Parameters

Look Back at Yesterday

Activity 2.4's countdown turned on three LEDs, one at a time, with the exact same two lines repeated for each.

led1.value(1)
time.sleep(1)
led2.value(1)
time.sleep(1)
led3.value(1)
time.sleep(1)

What's a Function?

A function is a named, reusable block of code — write the steps once, then run them again just by using the name.

Defining a Function

def starts a function definition. The word inside the parentheses, led, is a parameter — a placeholder for whatever gets passed in later.

def turn_on_and_wait(led):
led.value(1)
time.sleep(1)

Calling the Function

Each call passes in a different LED for the led parameter — same two lines of behavior, three different pins.

turn_on_and_wait(led1)
turn_on_and_wait(led2)
turn_on_and_wait(led3)

Identical behavior to yesterday. Far less repetition.

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, reusable block of code.
def
The keyword that starts a function definition.
Parameter
A placeholder variable in a function definition, filled in with a real value each time the function is called.
Call
Actually running a function by writing its name followed by parentheses.

'F' → Fullscreen

divider

Build

Using the same three-LED circuit from Activity 2.4, create a new MicroPython script named 2-5-functions.


Task 1: Define the Function

  • Set up the same three LEDs as Activity 2.4.
  • Define a function turn_on_and_wait(led) that turns the given LED on, then waits one second.
Writing Your Own Functions
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def turn_on_and_wait(led):
led.value(1)
time.sleep(1)

Task 2: Call It Three Times

  • Call turn_on_and_wait() once for each LED, recreating yesterday's countdown with far less repeated code.
Writing Your Own Functions
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def turn_on_and_wait(led):
led.value(1)
time.sleep(1)
turn_on_and_wait(led1)
turn_on_and_wait(led2)
turn_on_and_wait(led3)

Task 3: Add a Second Parameter

  • Add a wait parameter to the function so each call can use a different pause length.
  • Make the countdown accelerate: 1 second, then 0.7, then 0.4.
Writing Your Own Functions
from machine import Pin
import time
led1 = Pin(28, Pin.OUT)
led2 = Pin(29, Pin.OUT)
led3 = Pin(30, Pin.OUT)
def turn_on_and_wait(led, wait):
led.value(1)
time.sleep(wait)
turn_on_and_wait(led1, 1)
turn_on_and_wait(led2, 0.7)
turn_on_and_wait(led3, 0.4)
divider

Checkpoint

When you run your script:

  • All three LEDs should turn on in sequence, just like Activity 2.4
  • After adding the wait parameter, each LED should turn on a little faster than the one before it
divider

Reflection

Answer the following questions before submitting your work.

  1. What repetition did defining turn_on_and_wait() eliminate from your Activity 2.4 code?
  2. What does the parameter led represent inside the function definition, and how does it get a real value each time you call the function?
  3. Why might adding a second parameter like wait be useful beyond just this countdown?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete