Python Programming for the Raspberry Pi
You have already crossed paths with Python functions but you should learn a little more about them as you will be using them a lot. In fact you will also be creating your own functions.

What are Python functions and why do we need them?
Functions are small self-contained programs that perform a specific task. These functions are used by larger programs which have the benefit of making your program clearer and require less typing. This is because by create these functions you the ability to re-use code. The other advantage of this is that if you change the function then you do not have to change your main program because the function is being reference in your main program.

Python has a large amount of pre-defined functions and you use these functions by calling the function name. For example print(). Notice the parenthesis after the word print. This indicates that this is a function. Every time you call a function it will branch of to the function and run that section of its code and then return back to your main program.

If the functions program was to print hello followed by Mike then our program could like this.

printMike()
Hello Mike

Examine the function below

# The function below displays a name
def printMike():
print(“Hello Mike”);

In our main program we could get this to display Hello Mike 3 times.

printMike()
printMike()
printMIke()

Download hellomike.py but visiting this page and copying the contents into a blank file.

Now examine the entire program.

# A program to display a hello message

# The function below displays a name
def printMike():
print(“Hello Mike”)

printMike()

A comment appears at the start of the program describing the program which starts with the # symbol. Below this is another comment describing the what the following function does.

The def keyword is used to define a function and following this will be the function name and the parentheses. The colon is used to declare the start of an indented block of code.

NOTE: This function isn’t called yet as this will be done in the main program. This means that no output is displayed yet.

Finally the function is called using printMike(). Enter the above program into nano and save the file as helloMike.py. I will continue to use Nano until the programs becomes larger and then we will dive into an IDE.

Before we can run the program you will need to make sure that the file is executable. Enter the following.

$ chmod ugo+x hellomike.py

Run the program by entering the following:

$ python hellomike.py

Hello Mike

The great thing about functions is the we can now print a surname for mike without changing our main program. The next time we call printMike() it will print Hello Mike followed by his surname.

Now add printMike() three times.

# A program to display a hello message

# The function below displays a name
def printMike():
print(“Hello Mike”)

printMike()
printMike()
printMike()

Run the program. The result is Hello Mike displayed three times. Now edit the printMike() function so that Mike now has a last name.

def printMike():
print(“Hello Mike Powers”)

Run the program and note that it now displays Hello Mike Powers Hello Mike Powers Hello Mike Powers. Only one change was made to the function but the name was displayed three times. Function become extremely powerful when you are reusing code throughout your program. With only one change to the function you have managed to modify the program in three places.

Download the PDF file for Week 8 – Day 1 – Python Functions
Raspberry pi pdf download

raspberry-pi-week-8-day2-python-functions-parameters-next

Next Python Function Parameters – Day 2 >>