4. Functions with parameters
4.1. Using parameters
name
is the parameter, and "beginner"
and "user"
are the arguments.+
between the text strings.from microbit import *
def show_welcome(name):
display.scroll("Hello " + name, delay=60)
show_welcome("beginner")
show_welcome("user")
Tasks
Write a function called
ask_play_again
with a parameter for their player_name, and display an example using it.
Write a function called ask_play_again
with a parameter for their player_name, and display an example using it.
from microbit import *
def ask_play_again(player_name):
display.scroll("play again " + player_name + "?", delay=80)
ask_play_again("champ")
4.2. Functions with default parameters
+
between the text strings.str()
is used to turn score
, which is a integer, into a string.from microbit import *
def player_info(name="Novice", score=0):
display.scroll(name + " has a score of " + str(score), delay=80)
player_info()
player_info("Rookie", 100)
Tasks
Write a function called
player_health
with 2 default parameters for their user_name and their game health status and display an example using the defaults and an example passing values.
Write a function called player_health
with 2 default parameters for their user_name and their game health status and display an example using the defaults and an example passing values.
from microbit import *
def player_health(user_name="novice", health=100):
display.scroll(user_name + "has health of " + str(health), delay=80)
player_health()
player_health("speedy", 85)
4.3. Order with named parameters
from microbit import *
def player_info(name="Novice", score=0):
display.scroll(name + " a score of " + str(score), delay=80)
player_info(name="Rookie", score=10)
player_info(score=10, name="Rookie")
Tasks
Write a function called
player_health
with 2 default parameters for their user_name and their game health status and display an example using it with the parameter order mixed up.
Write a function called player_health
with 2 default parameters for their user_name and their game health status and display an example using it with the parameter order mixed up.
from microbit import *
def player_health(user_name="novice", health=100):
display.scroll(user_name + "has health of " + str(health), delay=80)
player_health(health=85, user_name="speedy")
4.4. Parameter order: positional before default
from microbit import *
def player_info(name, score=0):
display.scroll(name + " has a score of " + str(score), delay=80)
player_info("novice")
player_info("Rookie", 100)
Tasks
Write a function called
player_health
which takes the user_name as the first parameter and their game health status as a default parameter and display an example using it with and without passing a value to the default parameter.
Write a function called player_health
which takes the user_name as the first parameter and their game health status as a default parameter and display an example using it with and without passing a value to the default parameter.
from microbit import *
def player_health(user_name, health=100):
display.scroll(user_name + "has health of " + str(health), delay=80)
player_health("speedy")
player_health("speedy", 85)
Tasks
Write a function called
player_info
with 3 default parameters for their user_name, their number of game lives and their game health status and display an example using it.
Write a function called player_info
with 3 default parameters for their user_name, their number of game lives and their game health status and display an example using it.
from microbit import *
def player_info(name="novice", game_lives=3, health=100):
display.scroll(name + "has" + str(game_lives) + " lives with health of " + str(health), delay=80)
player_info()
player_info("speedy", 2, 65)
4.5. Functions returning information
from microbit import *
def convert_inches_to_centimetres(inches):
return inches * 2.54
length_cm = convert_inches_to_centimetres(8)
display.scroll(length_cm)
from microbit import *
def area_of_rectangle(length, width):
return length * width
area = area_of_rectangle(9, 7)
display.scroll(area)
+
between the text strings.str()
is used to turn age
, which is a integer, into a string.from microbit import *
def player_goals(name, goals):
return name + " scored " + str(goals) + " goals."
display.scroll(player_goals("Cristiano Ronaldo", 838), delay=70)
display.scroll(player_goals("Messi", 803), delay=70)
display.scroll(player_goals("Pele", 762), delay=70)
Tasks
Define a function
convert_cm_to_m(cm)
that returns the result of converting a length in cm to metres.Define a function
convert_m_to_cm(m)
that returns the result of converting a length in metres to cm.Define a function
area_square(length)
that returns the area of a square.Write a function called
random_greeting
that returns a random greeting that is randomly chosen from a list of greetings:["Hi", "Hello", "G'day"]
. See: https://www.w3schools.com/python/ref_random_choice.asp
Define a function convert_cm_to_m(cm)
that returns the result of converting a length in cm to metres.
from microbit import *
def convert_cm_to_m(cm):
return cm / 100
length_cm = convert_cm_to_m(80)
display.scroll(length_cm)
Define a function convert_m_to_cm(m)
that returns the result of converting a length in metres to cm.
from microbit import *
def convert_m_to_cm(m):
return m * 100
length_m = convert_m_to_cm(1.82)
display.scroll(length_m)
Define a function area_square(length)
that returns the area of a square.
from microbit import *
def area_square(length):
return length * length
area = area_square(5)
display.scroll(area)
Write a function called random_greeting
that returns a random greeting that is randomly chosen from a list of greetings: ["Hi", "Hello", "G'day"]
.
from microbit import *
import random
def random_greeting(name):
greetings = ["Hi", "Hello", "G'day"]
greet = random.choice(greetings)
return greet + " " + name
greeting = random_greeting("Jim")
display.scroll(greeting, delay=70)
4.6. Allowing for a variable number of arguments
*args
allow a function to take any number of positional arguments (non keyword arguments).*nums
allows a variable number of arguments to be passed in to be added in the multi_add
function.nums
has five arguments: 1, 3, 5, 7, 9.from microbit import *
def multi_add(*nums):
sum = 0
for num in nums:
sum = sum + num
return sum
display.scroll(multi_add(1, 3, 5, 7, 9), delay=70)
Tasks
Define a function
multi_product(*nums)
that finds the product of the first 4 primes.Define a function
multi_average(*nums)
that finds the average of the first 4 primes.
Define a function multi_product(*nums)
that finds the product of the first 4 primes.
from microbit import *
def multi_product(*nums):
total = 1
for num in nums:
total = total * num
return total
display.scroll(multi_product(2, 3, 5, 7), delay=70)
Define a function multi_average(*nums)
that finds the average of the first 4 primes.
from microbit import *
def multi_average(*nums):
sum = 0
for num in nums:
sum = sum + num
return sum/len(nums)
display.scroll(multi_average(2, 3, 5, 7), delay=70)