DUA
2
Objectives
◎ Revision
◎ Introduction to Functions
◎ Built in functions vs Self Defined
◎ Calculator with functions
◎ Reflection
◎ Complete the Quiz and Assignment
WIN-WIN AGREEMENT
• -Sign in using your Full Name and type present in the chat
box as soon as you enter.
• -Turn on your video.
• -Use pickers like raising hands or typing in chat box to clear
your queries during or last 5 minutes of your block according
to the teacher’s instructions.
• -Practice the learnt concepts.
• -Attempt the quiz and submit the assignment on time in
Google classroom.
Note: Zoom blocks are recorded
4
What is a function in Python?
• In Python, a function is a group of related statements
that performs a specific task.
• Functions help break our program into smaller and
modular chunks. As our program grows larger and
larger, functions make it more organized and
manageable.
• Furthermore, it avoids repetition and makes the code
reusable.
Built in Functions
input()
reads and returns a line of string
print()
Prints the Given Object
int()
Converts a string to an integer
float()
Converts a string to a float number
SYNTAX OF A USER DEFINED FUNCTION
Name of function Optional Parameters
Keyword to start
function definition
Colon to mark end of
def function_name(parameters): function header
"""docstring"""
statement(s)
Statements or code of Description of the
the function function
Example of a function
def greet(name):
""" This function greets to the person passed in as a parameter """
print("Hello, " + name + ". Good morning!")
How to use a function
greet('Paul’)
Addition Function
def add_function(num1, num2):
“””This function adds numbers”””
num3 = num1 + num2
return num3
A = 50
B = 25
C = add_function(A,B)
print(C)
Important Notes:
1. Variables assigned outside the function definition are
called global variables and can be used anywhere.
2. Variables assigned inside the function definition are
local variables and can not be used outside the function
definition.
3. Give the parameters to the function in the correct order
as your equation.