How to Create Your Own Functions in Python on the Raspberry Pi
One of the things the Raspberry Pi allows you to do in Python, and many other programming languages, is create a function. A function can receive some information from the rest of the program (one or more arguments), work on it, and then send back a result.
Before you can use a function, you have to define it, which you do using a def statement. To tell Python which instructions belong in the function, you indent them underneath the def statement. Here’s an example program to familiarize you with the idea of functions, and how we’ll be using it:
# Example of functions def dictionarycheck(message): print "I will look in the dictionary for", message return "hello" dictionarycheck("test message") result=dictionarycheck("test message2") print “Reply is:”, result
We’ll talk you through that program in a moment, but here’s a glimpse of what is shown onscreen when you run it:
I will look in the dictionary for test message I will look in the dictionary for test message2 Reply is: hello
This is a short but powerful program because it tells you nearly everything you need to know about functions. As you can see, the function was defined at the +
start of the program, with this line:
This sets up a function with the name dictionarycheck(), but also sets it up to receive a piece of information from the rest of the program and to put it into the variable we’ve called message.
The next line prints out a statement saying “I will look in the dictionary for” followed by the contents of the variable message. That means it prints out whatever information is sent to the function. The next line starting with return exits the function and sends a message back, which in our example is hello.
Functions are self-contained units so the variable message can’t be used by the rest of the program (it’s what’s known as a local variable). When you’re writing your own functions, you should give them a job to do, and then use return to send the result back to the rest of the program.
Functions aren’t run until you specifically tell the program to run them, so when Python sees the function definition, it just remembers it for when it needs it later. That time comes shortly afterwards, when you issue the command:
This runs our dictionarycheck() function, and sends it the text “test message” to work with. When the function starts, Python puts “test message” into the function’s variable called message, and then prints the text onscreen that contains it. The text “hello” is sent back by the function, but you don’t have a way to pick up that message.
The next code snippet shows you how you can pick up information coming back from a function. Instead of just running the function, you set a variable to be equal to its output, like this:
result=dictionarycheck("test message2") print “Reply is:”, result
When the text “hello” is sent back by the function, it goes into the variable result, and the main program can then print it on the screen.
This simple example illustrates a few reasons why functions are a brilliant idea, and have become fundamental building blocks in many programming languages:
Functions enable you to reuse parts of your program. For example, we’ve used our function to display two different messages here, just by sending the function a different argument each time. When you use more sophisticated programs, being able to reuse parts of your program makes your program shorter, simpler, and faster to write.
Functions make understanding the program easier because they give a name and a structure to a set of instructions. Whenever someone sees dictionarycheck() in our program, they can make a good guess at what’s going on. As you work on bigger projects, you’ll find readability becomes increasingly important.
It makes it easier to maintain and update your program. You can easily find which bits of the program to change, and all the changes you need to make will be in the same part of the program. If you think of a better way to do a dictionary look-up later, you just modify the function, without disturbing the rest of the program.
Functions make prototyping easier. That’s what we’ve done here: We’ve built an experimental program that takes some text and sends back a message. That’s what our finished dictionarycheck() function will do, except that this one just sends the same message back every time and the finished one will send different messages back depending on what the player said.
You could build the rest of the program around this prototype to check it works, and then go back and finish the dictionarycheck() function.