Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Defining a Function

Example 7.1. Defining a function

#!/usr/bin/python
# Filename: function1.py

def sayHello():
print 'Hello World!' # block belonging to the function
# End of function

sayHello() # call the function

Output

$ python function1.py Hello World!

How It Works

We define a function called sayHello using the syntax as explained above. This function takes no parameters and hence there are no variables declared in the parentheses. Parameters to functions are just input to the function so that we can pass in different values to it and get back corresponding results.