2. What you’ve learned?
What are loops and how it reduce the effort of reducing instructions
What is for loop and how to iterate with it
What is range() function and how to iterate through lists
How to iterate with while loop
4. Functions
• A function is a group of statements that together perform a task
• Functions are one way to reuse code—you can use functions in
your programs again and again.
5. Parts of a Function
• A function has three parts: a name, parameters, and a body.
Here’s an example of a simple function:
>>> def testfunc(myname):
print('hello %s' % myname)
6. Parts of a Function
• A function has three parts: a name, parameters, and a body.
Here’s an example of a simple function:
>>> def testfunc(myname):
print('hello %s' % myname)
Def is a python keyword
provided for
programmers to create a
python function
7. Parts of a Function
• A function has three parts: a name, parameters, and a body.
Here’s an example of a simple function:
>>> def testfunc(myname):
print('hello %s' % myname)
Testfunc is the name of
the function. We can
give any name for the
function as per our
convenience
8. Parts of a Function
• A function has three parts: a name, parameters, and a body.
Here’s an example of a simple function:
>>> def testfunc(myname):
print('hello %s' % myname)
Myname is called as the
parameter of the function. A
parameter is a variable that
exists only while a function is
being used.
9. Parts of a Function
• A function has three parts: a name, parameters, and a body.
Here’s an example of a simple function:
>>> def testfunc(myname):
print('hello %s' % myname)
This space indicates that we
are going to start a block.
Which is obviously the body
of the function. And in our
body there is only one
statement which is print()
10. Calling a function
>>> def testfunc(myname):
print('hello %s' % myname)
>>> testfunc('Mary')
Function body or definition
Calling the function
11. Calling a function
>>> def testfunc(myname):
print('hello %s' % myname)
>>> testfunc('Mary')
Function body or definition
Calling the function
Output
hello Mary
12. Calling a function
>>> def testfunc(myname):
print('hello %s' % myname)
>>> testfunc('Mary')
The value Mary will be
assigned to the variable
myname during the function
call
Output
hello Mary
13. Returning a value
• A function is often used to return a value, using a return
statement. For example, you could write a function to calculate
your average mark
>>>def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=averageMark(35,35,50)
>>>print(a)
40
14. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>> a=averageMark(35,35,50)
print(a)
Function body or definition
Calling the function
15. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=averageMark(35,35,50)
print(a)
If you call the function
averageMark() it will
calculate and return the
average
Calling the function
16. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=averageMark(35,35,50)
print(a)
This is where we call the
function averageMark().
We pass three
parameters 35,35,50
during function call
17. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=averageMark(35,35,50)
print(a)
This is where we call the
function averageMark().
We call it with three
parameters 35,35,50
18. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a = averageMark(35,35,50)
print(a)
It calculate and returns
the average value. Ie 40
19. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a = averageMark(35,35,50)
print(a)
So this selected potion
will be replaced with the
returned value. Ie 40
20. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=40
print(a)
So variable a will be
having a value 40
21. Example Returning a value
>>> def averageMark(science,english,maths)
return (science+english+maths)/3
>>>a=40
print(a) Output
40
23. Exercise !
• Create a function named “MyDetails()” , calling which will print all the
details about you
• Create a function to calculate the square of any number passed into it
• Crate a function that will return the square of any number passed in to it
25. What you’ve learned?
What is function ? And how it will help you to reuse your code
What is parameters? How to pass parameter in to a function
How to return a value from a function