Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781783551590
Pages 200 pages
Edition 1st Edition
Languages
Author (1):
Dan Nixon Dan Nixon
Profile icon Dan Nixon

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}