0% found this document useful (0 votes)
75 views

Python Functions

The document discusses functions in Python including built-in functions like print() and input() as well as user-defined functions with examples of defining a greeting function and addition function. Functions help organize code by breaking programs into modular, reusable chunks and avoiding repetition. The syntax for defining a function includes the def keyword followed by the function name and parameters, an optional docstring, statements that make up the function body, and the return statement.

Uploaded by

imaad8888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Python Functions

The document discusses functions in Python including built-in functions like print() and input() as well as user-defined functions with examples of defining a greeting function and addition function. Functions help organize code by breaking programs into modular, reusable chunks and avoiding repetition. The syntax for defining a function includes the def keyword followed by the function name and parameters, an optional docstring, statements that make up the function body, and the return statement.

Uploaded by

imaad8888
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

You might also like