Methods vs Functions
At this point you have used several methods to perform certain tasks with the micro:bit module. For example, scroll and show are methods that you used to make text, values, and images appear on the display of the micro:bit module.
You may recall that previously we have referred to these methods as functions and it is very common for people to do so. However, in Python, functions are technically different from methods. There are some fundamental differences between what Python calls a method and what Python calls a function.
Without getting into the details, think of methods as functions that belong to a specific object. For example the scroll method belongs to the display. Thus, we had to type display.scroll in order to use the scroll method, that is, the scroll method is dependent on the display object. Someone has already defined many objects for us, and the methods that belong to those objects. We get to use them by simply using the import statement from microbit import *.
Defining a Function
In contrast, a function is not dependent on an object, and you can define them early in your code. To use them, you simply type their names along with any inputs, just like with methods.
The following program defines one function. The smile function simply displays a smile on the microbit’s display. It doesn’t take any inputs nor does it return anything.
- Examine the function definition below.
#smile.py from microbit import * def smile(): display.show(Image.HAPPY) sleep(2000)
Notice that the syntax for defining functions begins with the keyword def. After a space you can then type the name of the function, such as smile, with a pair of parentheses () attached. After the parentheses, you will need to put a colon:. Just like with if… statements, for loops, and while loops, the very next line after a colon will need to be indented. The indented lines that follow are all part of the function. The indented lines are not executed unless the function is called upon.
- Enter, save, and flash smile.py to the micro:bit module.
The program doesn’t do anything! We defined a function but we didn’t call upon it.
When you call a function, you must type it exactly like you named it. It is case sensitive. Also, it must be called below the function definition.
- Add a call to the smile function at the end of smile.py, and re-flash.
#smile.py from microbit import * def smile(): display.show(Image.HAPPY) sleep(2000) smile()
The program smile.py didn’t have a very complex function. Let’s see what else we can do with functions! For example, we can nest if…else… statements inside functions.
- Enter, save, and flash emote.py to your micro:bit.
# emote.py from microbit import * def emote(feeling): if feeling == "happy": display.show(Image.HAPPY) elif feeling == "sad": display.show(Image.SAD) else: display.scroll("I don't understand that feeling. Try happy or sad.") sleep(2000) emote("happy")
In emote.py, our function now has an input. That input is either the word "happy" or the word "sad". When the function is called, it will use the nested if..elif..else statement to determine if the feeling is happy, sad, or not understandable!
- Replace the word "happy" with "sad" and re-flash the program onto the microbit. What do you expect to display on the screen?
While the function in emote.py has an input, or parameter, that we can pass through it, it still doesn’t have anything it returns. The functions in adding.py both have parameters.
- Enter, save, and flash adding.py to your micro:bit.
# adding.py from microbit import * def add_one(value): value = value + 1 return value def addTogether(value1, value2): result = value1 + value2 return result display.scroll(add_one(5)) display.scroll(addTogether(5,6))
Notice how the add_one function takes one input. It then takes the input and adds one to it. It then returns the new result of that addition.
The addTogether function takes two inputs. It then adds those two inputs together and assigns the result to a new variable called result. It then returns that result.
Since the neither of these functions cause the value to display, we can instead call them inside of the display’s scroll method. This means we can pass functions through methods!
Optional Example: ternary.py
Not only can we use functions as arguments, we can also write functions that have conditionals as parameters using what is called the ternary operator. The familiar binary operators have 2 inputs. The ternary operator has three inputs: input1 if input2 else input3. Note that input2 is a condition. To see the ternary operator in action, check out the last few lines of the following program
- Enter, save, and flash ternary.py to your micro:bit.
#ternary.py from microbit import * def add_one(value): value = value + 1 return value while True: computation = add_one(5 if button_a.is_pressed() else 10) display.scroll(computation)
- Press button A so the function will add one to 5, then release it so it will add one to 10.
The result is stored to the variable called computation, then the value of computation is scrolled on the display.