Microbit Python
Advanced
60 mins
Teacher/Student led
135 points
What you need:
Chromebook/Laptop/PC
Microbit

Function Junction

In this step-by-step lesson, you'll delve into the world of Python programming, learning the difference between procedures and functions. You'll create simple procedures and functions using a micro:bit, and understand how to use parameters. Finally, you'll apply your knowledge to build a micro:bit weather station, displaying different symbols for various temperature ranges.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, we'll explore the wonderful world of procedures and functions in Python programming. But first, let's clarify the difference between the two.

    Imagine you have a toolbox. Some tools in this box, like a hammer, perform actionsโ€”they don't give you anything back after you've used them; they just do their job. In the programming world, these are similar to procedures. Procedures are blocks of code that execute a series of instructions but don't return any value once they've run.

    On the other hand, other tools in your toolbox, like a measuring tape, provide you with a specific value or information after you've used them. These tools are akin to functions in programming. Functions, like the measuring tape, perform an action and then give you something in returnโ€” a value that you can use elsewhere in your program.



    2 - Procedures

    A procedure is a block of code that can be reused throughout a program. Think of it as a sequence of instructions bundled together under a common name. Unlike functions, procedures do not return values. They just perform an action.

    Let's create a simple procedure using the micro:bit to display a smiley face:

    from microbit import *
    
    def show_smiley():
        display.show(Image.HAPPY)
        sleep(1000)
        display.clear()
    
    show_smiley()
    

    In the above code, show_smiley() is a procedure that shows a smiley face on the micro:bit's display for one second and then clears the screen. We define the procedure using the def keyword and call it by its name followed by parentheses ().

    Create a new project and add & run the code.

    3 - Functions

    While procedures perform actions, functions can return values. This allows them to be more flexible in many scenarios. Functions in Python are defined using the def keyword, similar to procedures.

    Let's create a function that returns the square of a number:

    from microbit import *
    
    def square(number):
        return number * number
    
    result = square(4)
    display.scroll(str(result))
    

    Here, the function square() calculates the square of the provided number and returns it. When we call the function with an argument of 4, the micro:bit will scroll the result, which is 16.

    Replace the code in your project with this code and run it. Try passing different numbers into the square() function and run your code again.

    4 - Functions with Parameters

    Parameters allow us to pass information into our functions, making them more flexible. A function can have multiple parameters, separated by commas.

    Let's create a function that takes two numbers as parameters and returns their sum:

    from microbit import *
    
    def add_numbers(a, b):
        return a + b
    
    total = add_numbers(5, 3)
    display.scroll(str(total))
    

    In this example, our function add_numbers() accepts two parameters, a and b. When called with the arguments 5 and 3, the function returns 8, and the micro:bit scrolls this result on its display.

    Replace the code in your project with this code and run it. Try passing different numbers into the add_numbers() function and run your code again.

    5 - Project - Micro:bit Weather Station

    Imagine if your micro:bit could tell you how the weather feels outside? In this project, you'll be designing a simplified weather station. You'll input a temperature, and the micro:bit will display a corresponding symbol to indicate cold, moderate, or hot conditions!

    Create a new project.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    ๐Ÿช Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more