5.3. Functions

A function is a named collection of Python statements. Functions can take any number of arguments. The general form of a function is:

CD-ROM reference=5021.txt
def <function name>(<arguments>):
<statements>

In the following example, a function printHello is declared and then executed interactively.

CD-ROM reference=5022.txt
>>> def printHello():
... print "Hello"
...
>>> printHello()
Hello
>>>

Note that the print "Hello" line is indented by one space relative to the def printHello(): line. Python's interactive prompt changes to a ... when it is expecting to see indented lines. To stop entering indented lines, press RETURN on a blank line. Python's prompt then returns to >>>.

In the following example, a function HoursFromDays ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.