0% found this document useful (0 votes)
6 views38 pages

Chapter 2 - Working With Functions (Revised)

This document provides an overview of functions in Python, including their definition, types (built-in, modules, and user-defined), and how to use them with parameters and arguments. It also covers variable scope, including global and local variables, and includes a series of class tests to assess understanding of the material. The document serves as a reference for understanding the fundamental concepts of functions in Python programming.

Uploaded by

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

Chapter 2 - Working With Functions (Revised)

This document provides an overview of functions in Python, including their definition, types (built-in, modules, and user-defined), and how to use them with parameters and arguments. It also covers variable scope, including global and local variables, and includes a series of class tests to assess understanding of the material. The document serves as a reference for understanding the fundamental concepts of functions in Python programming.

Uploaded by

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

CHAPTER - II

WORKING WITH FUNCTIONS


REFERENCE BOOK
SUMITA ARORA
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
return add,diff also called as
formal
parameter
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)
print("Sum = ",a) Argument is also
print("diff = ",b) called as actual
main() parameter
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

These are the arguments passed to a function in


correct positional order
For example:
def substract(a,b):
print(a-b)
substract()
>>substract(100,200)
-100
>>substract(150,100)
50
2. DEFAULT ARGUMENTS

When a function call is made without


arguments, the function has defalut values for it for
example:
For example:
def greet_msg(name=“Mohan”):
print(“Hello “, name)

greet_msg(“Vinay”) # valid
greet_msg() #valid
2. DEFAULT ARGUMENTS
When a function call is made without
arguments, the function has default values for it
for example:
For example:
def greet_msg(name=“Mohan”,msg=“GoodMorning”):
print(name,msg)
greet_msg()

def greet_msg(name=“Mohan”,msg): # invalid


print(name,msg)
greet_msg()
3. KEYWORD ARGUMENTS

if function containing many arguments,


and we wish to specify some among them, then
value for such parameter can be provided by
using the name

For example:
def greet_msg(name,msg):
print(name,msg)
#calling function
greet_msg(name=“Mohan”,msg=“Hi”): # valid
O/p -> Mohan Hi
3. KEYWORD ARGUMENTS

For example:

#calling function
greet_msg(msg=“Hi”,name=“Mohan”): # valid
O/p -> Mohan Hi
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
ii) Local variables are accessed within the program
and their scope is local only.
GLOBAL VARIABLES & LOCAL VARIABLES
For Example:
GLOBAL VARIABLE def main():
x=9
LOCAL VARIABLES
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
Class Test

1. Which of the following is the use of function in


python?

a) Functions are reusable pieces of programs


b) Functions don’t provide better modularity for
your application
c) you can’t also create your own functions
d) All of the mentioned
2. Which keyword is use for function?
a) Fun b) Define
c) def d) Function
Class Test

3. What is the output of the below program?


def sayHello():
print('Hello World!')
sayHello()
sayHello()

a) Hello World!
Hello World!
b) ‘Hello World!’
‘Hello World!’
c) Hello
Hello
Class Test

4. What is the output of the below program?


def printMax(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
printMax(3, 4)
a) 3 b) 4
c) 4 is maximum d) None of the mentioned
Class Test

5. What is the output of the below program ?


x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
a) x is now 50 b) x is now 2
c) x is now 100 d) None of the mentioned
Class Test

6. What is the output of the below program?


x = 50 a) x is 50
def func(): Changed global x to 2
global x Value of x is 50
print('x is', x) b) x is 50
x=2 Changed global x to 2
print('Changed global x to', x) Value of x is 2
func() c) x is 50
print('Value of x is', x) Changed global x to 50
Value of x is 50
d) None of the mentioned
Class Test

7. What is the output of below program?


def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
a) Hello
WorldWorldWorldWorldWorld
b) Hello c) Hello
World 5 World,World,World,World,
World
d) Hello
HelloHelloHelloHelloHello
Class Test

8. What is the output of the below program?


def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
b) a is 3 and b is 7 and c is 10
func(3, 7)
a is 5 and b is 25 and c is 24
func(25, c = 24)
a is 50 and b is 100 and c is 5
func(c = 50, a = 100)
c) a is 3 and b is 7 and c is 10
a) a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
a is 5 and b is 100 and c is 50
d) None of the mentioned
Class Test

9. What is the output of below program?


def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y

print(maximum(2, 3))
a) 2 b) 3 c) The numbers are equal
d) None of the mentioned
Class Test

1 A
2 C
3 A
4 C
5 A
6 B
7 A
8 C
9 B
#Find the output of the following
def change(P,Q=30):
P=P+Q
Q=P-Q
print(P,'#',Q)
return P

A=150
B=100
A=change(A,B)
print(A,'#',B)
B=change(B)
#Find the output of the following
def fun(s):
k=len(s)
m=''
for i in range (0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif (s[i].isalpha()):
m=m+s[i].upper()
else:
m=m+'bb'
print(m)

fun('@gmail.com')
Thank You

You might also like