In this third part of a six-part series on the Python programming language, we'll take a look functions. This article is excerpted from chapter two of the book Beginning Game Development with Python and Pygame: From Novice to Professional, written by Will McGugan (Apress; ISBN: 9781590598726).
Understanding Functions
A function is a stored piece of Python code that you can pass information to and potentially get information back from. Python provides a large number of useful functions (see Table 2-4 for some examples), but you can also create your own.
Table 2-4. Some Built-in Python Functions
Function
Description
Example
abs
Finds the absolute value of a number
abs(-3)
help
Displays usage information for any Python object
help([])
len
Returns the length of a string or collection
len("hello")
max
Returns the largest value
max(3, 5)
min
Returns the smallest value
min(3, 4)
range
Returns a list containing a range of numbers
range(1,6)
round
Rounds a float to a given precision
round(10.2756, 2)
* For a more comprehensive list of Python’s built-in functions, see the Python documentation, or visit http://doc.python.org.
Defining Functions
To define a function in Python, you use the def statement followed by the name you want to give to your function. You can use any name you want, but it is a good idea to give it a name that describes what it actually does! Function names are typically in lowercase and may use underscores to divide words. Listing 2-2 is a simple Python function for calculating the tip on a meal of fugu.
Listing 2-2. Calculating the Tip on Fugu
def fugu_tip(price, num_plates, tip): total = price * num_plates tip = total * (tip / 100.) return tip
When Python first encounters adef statement, it knows to expect a function definition, which consists of the name of the function followed by a list of parameters in parentheses. Just as withfor,while, andifstatements, a colon is used to introduce a block of code (known as the function body). In the aforementioned statements, the block of code doesn’t run immediately—it’s just stored away until it is needed. Calling the function causes Python to jump to the beginning of the function body and assign information given in the call to each of the parameters. So in Listing 2-2, the first call tofugu_tipruns withpriceset to 100,num_platesset to 2, andtipset to 15.
The only thing you haven’t already encountered infugu_tipis thereturnstatement, which tells Python to jump back from the function, potentially with some new information. In the case offugu_tipwe return with the value of our tip, but a function can return any Python object.
Note You don’t need areturnstatement in the function. Without areturnstatement, a function will return when it gets to the end of the code block with the valueNone—which is a special Python value indicating “nothing here.”
You may have noticed that insidefugu_tiptwo variables are created; these are called local variables, because they only exist inside the function. When the function returns,totalandtipwill no longer exist in Python’s memory—although it is possible to have variables with the same name outside of the function.