Functions¶
Functions and methods are used in programming to ‘parcel up’ useful snippets of code and use them whenever we want. Python has many built-in functions like sleep() and int(), but you can make your own to re-use sections of code in your programs.
Functions make code easier to read and modify. Define your function near the start of your program, giving it a descriptive name, then call it using its name followed by round brackets.
Procedures¶
Procedures, also called sub-routines, are functions that carry out a fixed set of instructions.
def heart_wait():
display.show(Image.HEART)
sleep(1000)
heart_wait()
display.clear()
This function called heart_wait() shows a heart on the micro:bit’s LED display, then waits 1 second.
Functions with parameters¶
You can put data in brackets to pass to a function, which the function can then process.
def image_wait(myImage):
display.show(myImage)
sleep(1000)
image_wait(Image.HEART)
image_wait(Image.HEART_SMALL)
In this example, we define a function called image_wait(). This will show any image we pass to the function.
Data you pass to a function are called parameters. Parameters make functions more flexible.
You can send more than one parameter to a function. This function allows you to specify both the image and how long the function waits. The default delay is 1 second, but you can over-ride it by specifying a new time when you call the function:
def image_wait(myImage, delay=1000):
display.show(myImage)
sleep(delay)
image_wait(Image.HEART)
image_wait(Image.HEART_SMALL, delay=3000)
display.clear()
Functions with return¶
Functions can process information and return it.
def convertCtoF(c):
return c * 1.8 + 32
display.scroll(convertCtoF(temperature()))
This function takes any number you pass to it, modifies it using the formula to convert centigrade to Fahrenheit, and returns it.
The function convertCtoF() is called inside a display.scroll() statement. The current temperature in centigrade is passed to the function.
‘Function’ is a mathematical term describing a relation or expression involving one or more variables, such as F = C x 1.8 + 32.
Functions in computer programs work in a similar way, which is why they are good for performing calculations and conversions.
Scope of variables¶
Variables aren’t always available to all parts of your program. Sometimes this is what you want, but it can cause problems. Variables can be global or local.
Global variables can be accessed by any part of your program. Local variables only work inside a section of your program such as a function
Scope: global variables¶
If you create a variable outside a function definition, it will be global and can be used by the function.
def greeting():
display.scroll('Hello ' + name)
name = 'Sam'
greeting()
In this example the display shows ‘Hello Sam’.
Although name is a global variable, it must be defined before the function greeting() is called, or you will get an error.
If you want to modify the value of a global variable in a function, you must declare that you want to use the global variable of that name using global.
Here we use the global variable count to track how many times the function has been called:
def clicker():
global count
count += 1
display.show(count)
count = 0
while True:
if button_a.was_pressed():
clicker()
Scope: local variables¶
Variables inside functions are assumed to be local unless you state otherwise.
def count():
number = 9
display.show(number)
number = 5
count()
sleep(1000)
display.show(number)
Here we have two instances of a variable called number. Inside the count() function number is a local variable with a value of 9.
Outside the function there is a global variable also called number with a value of 5.
You might expect that calling the function would change the value of number to 9 and show 9 twice. But in fact this will show 9 and then 5 on the display because we have two different variables both called number. One is local to the function and has the value 9, the other is global and has the value 5.
Exercises¶
- create a program with a function to greet the person ‘Hello Friend!’
- create a program with a function that takes in the name and the age and then prints out ‘Happy Birtday’ to that person and prints out the age.
- Display the Fibonacci series using a function named
fibwith a parameter that sets the maximum value of the series. Fibonacci series is defined as a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc. - Make a coin flipping program that will display either heads or tails. Use random function (50% chance of heads or tails) to determine the outcome of the coin flip. Make use of function.