113270 Views
93709 Views
85773 Views
53388 Views
50571 Views
48758 Views
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Making a Custom PCB for BurgerBot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
85% Percent Complete
By Kevin McAleer, 3 Minutes
At some point you will want to make a block of code that you will use many times, perhaps with different values. This is not only essential to save repeating the same block of code over and over again, its a better technique to improve reliability and readability.
In MicroPython we use the def keyword to define the code that follows as a function.
def
Functions can also have parameters of there own.
Lets create a simple function that will print a message when its called:
def say_hello(): # This function prints the message hello print('hello') say_hello()
The example above defines (thats why its the def keyword) the function called say_hello, notice that we also need to provide the () brackets as functions need this, we also need to use the : colon symbol to tell MicroPython that the following block of code relates to this function.
say_hello
()
:
The function code is also indented.
We often want our function to take in an input variable so that we can use it in the function code. Lets extend our say_hello example so that it can include a name and age.
def say_hello(name, age): # This function prints the message hello print('hello', name, 'how does',age,'feel?') say_hello('Kev', 47)
In the example above we’ve added two parameters name and age. Our functions can use these parameters within its block of code, in this case we use it to print out the message hello Kev how does 47 feel?. Notice how Python accepts the two different types of variables - the name is a string (Str) of text, and age is an integer (a whole number with no decimal points).
name
age
hello Kev how does 47 feel?
Str
We can take this a step further and pass variables to the function and it will work just the same:
def say_hello(name, age): # This function prints the message hello print('hello', name, 'how does',age,'feel?') my_name = 'Kev' my_age = 47 say_hello(my_name, my_age)
Functions can also call other functions, though on a MicroController you may find that the device crashes if it runs out of memory, if you nest too many functions, but that will take quite a few levels.
Functions can do more than just run blocks of code, they can return values too. Lets make a new function that takes a value and adds 1 to it.
1
def add_one(number): number += 1 return number a = add_one(1) print(a)
In the example above we define the function add_one which has one parameter number. It then adds 1 to the number and returns the number to the caller.
add_one
number
return
We assign a to the result of running add_one(1), which should return the value 2. Therefore the variable a should contain the value 2. Run the code and try passing different values and changing the number += 1 to different values.
a
add_one(1)
2
number += 1
Challenge Write your own function that takes a value, multiplies it by 10, and then returns that value.
Write your own function that takes a value, multiplies it by 10, and then returns that value.
10
< Previous Next >