More Related Content
Similar to Python Programming Essentials - M17 - Functions (20)
More from P3 InfoTech Solutions Pvt. Ltd. (20)
Python Programming Essentials - M17 - Functions
- 5. © SkillBrew http://skillbrew.com
Defining a function
5
def functionName():
# do something
def printList():
num_list = range(3)
print num_list
1. def keyword is used to define a function
2. def keyword is followed by a function name
3. Function name is followed by parenthesis ()
4. After this a block of python statements
- 6. © SkillBrew http://skillbrew.com
Calling a Function
6
def printList():
num_list = range(3)
print num_list
printList()
Output:
[0, 1, 2]
To call a function, we specify the function name
with the round brackets
Call to function
- 7. © SkillBrew http://skillbrew.com
return keyword
7
def printList():
num_list = range(3)
return num_list
my_list = printList()
print my_list
Output:
[0, 1, 2]
1. The return keyword is
used to return values
from a function
2. A function implicitly
returns None by default
even if you don’t use
return keyword
- 8. © SkillBrew http://skillbrew.com
Parameterized functions
8
def printList(upper_limit):
num_list = range(upper_limit)
print "list: %s" % num_list
printList(5)
printList(2)
Output:
list: [0, 1, 2, 3, 4]
list: [0, 1]
1. Names given in the
function definition
are called
parameters
2. The values you
supply in the
function call are
called
arguments
- 9. © SkillBrew http://skillbrew.com
Default arguments
9
def printList(upper_limit=4):
print "upper limit: %d" % upper_limit
num_list = range(upper_limit)
print "list: %s" % num_list
printList(5)
printList()
Output:
upper limit: 5
list: [0, 1, 2, 3, 4]
upper limit: 4
list: [0, 1, 2, 3]
value 4
1. When function is called
without any arguments default
value is used
2. During a function call if
argument is passed then the
passed value is used
- 11. © SkillBrew http://skillbrew.com
Keyword arguments
11
def printList(upper_limit, step=1):
print "upper limit: %d" % upper_limit
num_list = range(0, upper_limit, step)
print "list: %s" % num_list
printList(upper_limit=5, step=2)
Output:
upper limit: 5
list: [0, 2, 4]
range() function has two variations:
1.range(stop)
2.range(start, stop[, step])
- 12. © SkillBrew http://skillbrew.com
Keyword arguments (2)
12
printList(step=2, upper_limit=5)
printList(upper_limit=5, step=2)
Advantages of using keyword arguments
Using the function is easier since you don’t have to worry
about or remember the order of the arguments
- 13. © SkillBrew http://skillbrew.com
Keyword arguments (3)
13
You can give values to only those parameters which you
want, provided that the other parameters have default
argument value
printList(upper_limit=5)
- 15. © SkillBrew http://skillbrew.com
global variables
15
counter = 10
def printVar():
print counter
printVar()
print "counter: %d" % counter
Output:
10
counter: 10
Variable defined outside a function or class is a
global variable
counter is a
global variable
- 16. © SkillBrew http://skillbrew.com
global variables (2)
16
counter = 10
def printVar():
counter = 20
print "Counter in: %d" % counter
printVar()
print "Counter out: %d" % counter
Output:
Counter in: 20
Counter out: 10
counter in function definition is
local variable tied to function’s
namespace
Hence the value of global
counter does not gets modified
- 17. © SkillBrew http://skillbrew.com
global variables (3)
17
counter = 10
def printVar():
global counter
counter = 20
print "Counter in: %d" % counter
printVar()
print "Counter out: %d" % counter
Output:
Counter in: 20
Counter out: 20
In order to use the global
counter variable inside the function
you have to explicitly tell python to use
global variable using global keyword
- 18. © SkillBrew http://skillbrew.com
docstrings
18
def add(x, y):
""" Return the sum of inputs x, y
"""
return x + y
print add.__doc__
Python documentation strings (or docstrings) provide a
convenient way of associating documentation with Python
modules, functions, classes, and methods
docstring
Access a docstring using
object.__doc__
- 19. © SkillBrew http://skillbrew.com
Summary
What is a function
Advantages of using
functions
Defining a function
Calling a function
return keyword
19
Parameterized functions
Default arguments
Keyword arguments
global variables
docstrings
- 20. © SkillBrew http://skillbrew.com
Resources
Default arguments
http://www.ibiblio.org/g2swap/byteofpython/read/default-argument-
values.html
Keyword arguments
http://www.ibiblio.org/g2swap/byteofpython/read/keyword-
arguments.html
return keyword
http://www.ibiblio.org/g2swap/byteofpython/read/return.html
local variables http://www.ibiblio.org/g2swap/byteofpython/read/local-
variables.html
Docstrings
http://www.ibiblio.org/g2swap/byteofpython/read/docstrings.html
20