You can already run Python on the micro:bit and make the display show images or scroll text.
What is one reason you might want to give a block of code its own name and use it more than once?
Today you will write and call your first procedures and functions in Python, then build a simple weather station that picks a cold, moderate or hot symbol from a temperature you give it.
Keep this opening short. Devices should already be on the Python micro:bit editor. Ask the recap question to the whole class, then state what they will make today. Optional predict: before anyone runs later code, ask what they think a named block will do when it is called, and park one or two answers to revisit in make sense. Organise students in pairs at devices once you move on.
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.
This is the concept lead-in. Read or paraphrase the toolbox idea on the board. Do not rush past the difference: procedures do a job, functions can also hand a value back. Key question: when would you want a value back, and when is an action enough? Watch for students treating the two words as the same thing.
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 ().
PRIMM code-along. Model the def header and the call on the board before students paste or type. Predict: what will the display do when show_smiley is called? Run, then investigate. Common bug: defining the procedure but forgetting the call underneath, so nothing appears. Differentiation: support students by checking indentation under def first.
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.
square() function and run your code again.Stay in PRIMM. Ask what square(4) should hand back before anyone runs it. Model the return line and the use of the result on the board. Common bug: printing or scrolling inside the function instead of returning the value, or forgetting to convert the result to a string for the display. Cue stronger students to try a second call with a different argument.
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.
add_numbers() function and run your code again.Highlight parameters as the inputs that make one function flexible. Predict add_numbers(5, 3) as a class, then run. Common bug: wrong argument order or only one argument supplied. Key question: why is a function with parameters more useful than hard-coding the numbers inside? Pair talk on that question, then take two answers.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.