Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By : Dan Nixon
Book Image

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

By: Dan Nixon

Overview of this book

Table of Contents (18 chapters)
Getting Started with Python and Raspberry Pi
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using functions


The final topic we will cover in this chapter is that of separating code into functions, and how variables can be passed to and returned from them.

We will first start with a simple function that takes no arguments and does not return a value. The following example shows the basic syntax for a function in Python:

def say_hello():
    print "Hello!"
say_hello()

This example simply prints Hello! to the terminal, as shown in the following output:

In the next example, we will introduce an argument which will be used in the string. As the example shows, all that is needed is the argument name unlike the other programming languages, which require a type.

def say_hello(person_name):
    print "Hello, %s!" % (person_name)
say_hello("Sanae")

Tip

If you have a function that only operates on a specific type, you can still enforce arguments to take a specific type using the isinstance() function.

Here the string Hello, Sanae! is printed as shown in the following screenshot:

Like many other programming...