0% found this document useful (0 votes)
13 views29 pages

Functions by Monika 2024

Uploaded by

Dhimanrishit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views29 pages

Functions by Monika 2024

Uploaded by

Dhimanrishit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

FUNCTIONS

FUNCTIONS
What is Function?
What is Function?

A function is a group of statements that is


executed when it is called from some point of the
program.

It’s a divide and conquer approach


TYPES OF FUNCTIONS
TYPES OF FUNCTIONS

Functions can be categorized into three types

1) Built in Functions.

2) Modules.

3) User - defined functions.


TYPES OF FUNCTIONS

1) BUILT IN FUNCTIONS

These are predefined function in python and


are used as and when there is need by simply calling
them. For example:

int()
float()
str()
min()
max() ...etc
TYPES OF FUNCTIONS

2) MODULES

Module is a container of functions,


variables, constants, class in a separate file which
can be reused.
IMPORTING MODULES

i) Import statement
ii) from statement
IMPORTING MODULES – import STATEMENT

i) import statement : used to import


entire module.

Syntax: import modulename

example: import math


IMPORTING MODULES – import STATEMENT

ii) from: import all functions or selected


one.
Syntax:
from module name import function name

for example:
from random import randint
3) USER DEFINED FUNCIONS

Function is a set of statements that


performs specific task.

Syntax of user defined function

def function_name(list of parameters)


................
................
Statements def is keyword
3) USER DEFINED FUNCIONS

def sum_diff(x,y):
add=x+y
diff=x-y
return add,diff

def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()
PARAMETERS AND ARGUMENTS IN FUNCTION

Parameters are the values which are


provided at the time of function definition.
def sum_diff(p,q): Parameters
add=p+q
diff=p-q
Parameter is also
return add,diff called as formal
parameter or
formal argument
PARAMETERS AND ARGUMENTS IN FUNCTION

Arguments are the values which are passed


while calling a function
def main(): Arguments
x=9
y=3
a,b=sum_diff(x,y)
Parameter is also
print("Sum = ",a) called as formal
print("diff = ",b) parameter or
main() formal argument
TYPES OF ARGUMENTS

Python supports following type of


arguments:

1. Positional arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments
1. POSITIONAL ARGUMENTS
1. POSITIONAL ARGUMENTS
2. DEFAULT ARGUMENTS
2. DEFAULT ARGUMENTS

When a function call is made without


arguments, the function has default values for
it for example:
3. KEYWORD ARGUMENTS
3. KEYWORD ARGUMENTS
4. VARIABLE LENGTH ARGUMENTS

In some situation one needs to pass as


many as argument to a function, python
provides a way to pass number of argument
to a function, such type of arguments are
called variable length arguments.
Variable length arguments are defined
with * symbol.

For Example: (next slide)


4. VARIABLE LENGTH ARGUMENTS

For Example:
def sum(*n):
total=0
for i in n:
total+=i
print(“Sum = “, total)
sum()
# Calling function
sum() o/p sum=0
sum(10) o/p sum=10
sum(10,20,30,40) o/p sum=100
PASSING ARRAYS/LISTS TO FUNCTIONS

Arrays in basic python are lists that contain


mixed data types and can be passed as an argument
to a function.
For Example: # Arithmetic mean of list
def list_avg(lst):
l=len(lst) def main():
sum=0 print(“Input integers”)
a=input()
for i in lst:
a=a.split()
sum+=i for i in range(len(a) ):
return sum/l a[i]=int(a[i])
avg=list_ave(a)
print (“Average is = “, avg)
SCOPE OF VARIABLES

Scope mean measure of access of


variable or constants in a program. Generally
there are two types of scope of variables:

i) Global (Module)

ii) Local (Function)

i) Global variables are accessed throughout the


program their scope is global
GLOBAL VARIABLES

def main():
x=9
For Example:
y=3
m=100 a,b=add_diff(x,y)
def add_diff(x,y): print("Sum = ",a)
add=x+y print("diff = ",b)
diff=x-y print("m in main function = ",m)
global m main()
m= m +10
print("m in add_diff function=",m)
return add,diff
Thank You

You might also like