4. Python Functions
Functions are a core concept of programming. Functions allow us to group code together and tag that code with a name. This allows us to reuse code and avoid repeating ourselves.
Functions also break up our code into smaller, more manageable pieces. This is important for debugging and testing. Functions also allow us to create libraries of code that we can use in other programs. This is important for sharing code and collaborating with others. It can be useful to think of functions as mini-programs that we can call from of main program when we need them.
4.1 Calling a Function
We can then call that function. Calling a function means executing the code that was grouped together. Importantly, we can call the same function many times.
We have already used functions previously. For example, the Ed.Drive() function is a built-in function. Python has already grouped together or, defined, the code that prints to the screen. We then call the Ed.Drive() function thus executing some predefined code.
Notice that Ed.Drive() has brackets/parenthesis () after it. This part of the syntax of calling a function. The brackets are used to pass in any information that the function needs to do its job. For example, the Ed.Drive() function needs to know; type of movement, speed and amount. We can pass this information in as follows:
1 | |
What are some other built-in functions? That we have used?
4.2 Defining a Function
We can also define our own functions. This is done using the def keyword. Like other control structures in Python, we add a colon : to the end of the line and indented lines below belong to the structure. The syntax is as follows:
1 2 | |
The function_name is the name we want to give our function and, therefore, what we call it by. A function name should describe the purpose of the function. The code that is indented below the function name is the code that will be executed when we call the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
To call the above functions, we simply use the function name followed by brackets:
1 2 3 4 5 | |
Functions can be called multiple times which saves a us replicating the code used every time the function is called. In the above the Edison will do small_dance() twice, but the code for small_dance() only needs to written once (lines 11–14).
Does a function need to be defined before it is called? Why?
4.3 Parameters
Functions can also take in information. This is done by adding parameters to the function definition. Think of parameters as the inputs to our mini-programs. Parameters are variables that are passed into the function when it is called. This allows us to make our functions more flexible and reusable. The syntax for adding parameters is as follows:
1 2 | |
The parameters are separated by commas. We can then use these parameters in the code that is executed when the function is called. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
4.4 Output - Returning Values
Just like a program a function can have an output. This is done using the return keyword. The return keyword is used to output a value from a function. This can be thought of as a signal or message being sent back to where the function was called.
This allows us to use or store the result of a function to use later.
An example of this in a pre-built function is the Ed.ReadKeypad() function. Ed.ReadKeypad() detects if any buttons on the Edison are pressed. It returns a key value if something is pressed or NONE if nothing is pressed. For example:
The return keyword is used to output a value from a function. This allows us to use or store the result of a function to use later. For example:
1 2 3 4 5 6 7 8 9 10 11 | |
This will play a beep when the round button on the Edison is pressed. Don't worry about the syntax and how it does it, we'll revisit that at a later date.
What's important here is that on line 11, Ed.ReadKeypad() returns a value, if the value is Ed.KEYPAD_ROUND, we then make a decision to play a beep sound.
We can also make our custom functions return values. This is useful if we need calculations done and want the result. For example, from a previous example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
We can obtain length_3 by using a function rather than just adding length_1 and length_2 on line 11:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |