Pages

Wednesday, March 3, 2010

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.

No comments:

Post a Comment