0% found this document useful (0 votes)
36 views11 pages

Working With Functions

Here is the program with explanation: Before function call The value of a is: [10, 20, 30] In the function: The values of a is: [10, 20, 30, 40, 50, 60] The value of new list is: [10, 20, 30, 40, 50, 60] After the function call The value of a is: [10, 20, 30, 40, 50, 60] When the list is passed as an argument to the function, it is passed by reference. This means that the variable 'a' inside the function now refers to the same object as the variable 'a' outside the function. Any changes

Uploaded by

dalefav101
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)
36 views11 pages

Working With Functions

Here is the program with explanation: Before function call The value of a is: [10, 20, 30] In the function: The values of a is: [10, 20, 30, 40, 50, 60] The value of new list is: [10, 20, 30, 40, 50, 60] After the function call The value of a is: [10, 20, 30, 40, 50, 60] When the list is passed as an argument to the function, it is passed by reference. This means that the variable 'a' inside the function now refers to the same object as the variable 'a' outside the function. Any changes

Uploaded by

dalefav101
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/ 11

WORKING WITH FUNCTIONS

Definition:
Function- is a subprogram that acts as an on data and returns a value.

Three types:
1. Pre-defined functions
2. Functions with module
3. User- defined functions

Defining functions in Python


General syntax:

def func_name():
statements;
statements;
fun_name() # function call

Flow of Execution
Order of execution which statements are executed during a program run.

Arguments and Parameters


Parameters: The variables that are used during function definition are called
parameters/ Formal Parameters.

Arguments: The values that are being received during function call are called
Arguments/ Actual Parameters.
Eg: def add(a,b):
print(a*b)
y=3
add(12,y)

a,b formal parameters


12,7 actual parameters/arguments

Passing parameters
1. Positional Arguments:
When the function call statement must match with number and order of
arguments as defined in the function definition.

Eg: def check(a,b,c):


print()

check(1,2,3)/check(x,y,z)/check(2,x,y)

2. Default Arguments:
A parameter having default value in the function header/definition .

Note: any parameter cannot have default value unless all parameters appearing
on its right have their default value.

Eg: def interest(principal,time,rate=0.10):


# function call
si_int=interest(6100,2)# no value assigned for rate, so it takes default value from
function header
# function call
si_int=interest(6100,2,0.12)# default value is used only when argument is missing

def interest(principal=5000,time,rate=0.10):# invalid because right of its should


have default value
3. Keyword Arguments:
named arguments with assigned values being passed in the function call
statement.
no order to be maintained .
User can write any order, provided with name to arguments.

Eg:
interest(time=2,rate=0.10,principal=6000)

Returning values from functions:

1. Function returning some values- non-void functions


2. Function not returning values- Void functions

Return value can be:


literal,expression,variable

Eg:

return 5
return a
return a+b

Method-1
(without parameter/arguments, no return type)

ded add():
a=int(input("Enter number1:"))
b=int(input("Enter number 2:"))
c=a+b
print(c)
add()
Method-II
(with parameter/argument, no return type)

1def add(a,b):
2 c=a+b
3 print(c)
4
5 # function
6 a=int(input("Enter number1:"))
7 b=int(iput("Enter number2:"))
8 add(a,b)

order- 6,7,8,1,2,3

Method-III
(with parameter and argument return)

1 def add(e,f):
2 c=e+f
3 return c
4
5#function
6 a=int(input("Enter a number:"))
7 b=int(input("Enter another number:"))
8 d=add(a,b)
9 print(d)

order- 6,7,8,1,2,3,8,9
Method-IV
( no argument, no paramete, only return)
1 def add():
2 a=int(input("Enter a number:"))
3 b=int(input("Enter another number:"))
4 c=a+b
5 d=a*b
6 e=a/b
7 return c,d,e
8
9# function
10 p,q,r=add()
11 print(p,q,r)

order-10,1,2,3,4,5,6,7,10,11

Working of default parameter:

1 def add(a,b=100):
2 c=a+b
3 d=a*b
4 e=a/b
5 return c,d,e
6
7 # function
8 a=int(input("Enter number:"))
9 b=int(input("Enter another number:"))
10 p,q,r=add(a) #
11 print(p,q,r)

order- 8,9,10,1,2,3,4,5,10,11
working of named argument

1 def add(a,b=100):
2 c=a+b
3 d=a*b
4 e=a/b
5 return c,d,e
6
7 # function
8 x=int(input("Enter number:"))
9 y=int(input("Enter another number:"))
10 p,q,r=add(b=y,a=x)
11 print(p,q,r)

Scope of Variables:
1. local scope-within function
2. global scope-through out the program

Working: 1
1 def add(a,b):
2 c=a+b
3 d=b*2
4 print("the values inside the function:")
5 print(a,b)
6 return c,d
7
8 # function
9 a=int(input("Enter number:"))
10 b=int((input("Enter another number:"))
11 p,q=add(a,b)
12 print(p,q)
13 print("Values of a and b outside the function:")
14 print(a,b)
output:
value inside
12
34
value outside
12

Working: 2
1 def add(a,b):
2 a=a+b#
3 b=b*2#
4 print("the values inside the function:")
5 print(a,b)
6 return a,b
7
8 # function
9 a=int(input("Enter number:"))
10 b=int((input("Enter another number:"))
11 p,q=add(a,b)
12 print(p,q)
13 print("Values of a and b outside the function:")
14 print(a,b)

output:
value inside
34
34
value outside
12

LEGB rule:
local enclosed global built in

python determines how and when the variable or function can be used.
order is local -> enclosed -> global -> built in
Working: Global- varibale once made global will have the changes through out the
code

1 def add(x,y):
2 global a,b
3 a=a+2
3 b=b*2
4 print("the values inside the function:")
5 print(a,b)
6 return a,b
7
8 # function
9 a=int(input("Enter number:"))
10 b=int((input("Enter another number:"))
11 p,q=add(a,b)
12 print(p,q)
13 print("Values of a and b outside the function:")
14 print(a,b)

output:
value inside
34
34
value outside
12
Predict the output

1 value=50
2 def display(n):x=
3 global value
4 value=25
5 if n%7==0:
6 value =value+n
7 else:
8 value=value-n #
9
10 print(value,end='#')
11 display(20)
12 print(value)

order-- 1,10,11

output: 50 # 5

Predict the output:

def add(num1 + num2):


sum=num1+num2
sum=add(20,30)
print(sum)

Output :
50
predict the output

def call():
i=9
while i>1:
if(i%2==0):
x=i%2
i=i-1
else:
i=i-2
x=i
print(x**2)

Output
Loop1: Loop2: 7>1 Loop3: 5>1 loop4 :3>1
9>1 now i is 7 now i is 5 now i is 3
9%2==0 false 7%2==0 false 5%2==0 false 3%2==0 false
so else part so else part so else part so else part
i=9-2=7 i=7-2=5 i=5-2=3 i=3-2=1
x=i=7 new i value x=i=5 new i value x=i=3 new i value x=i=1 new i value
7**2=49 5**2=25 3**2=9 1**2=1
Write a program to explain the working of list argument in a function getting
assigned to an other list.

def add(a):
newl=a
newl.extend([40,50,60])
print("In the function")
print("The values of a is:",a)
print("The value of new list is:",newl)
a=[10,20,30]
print("Before function call")
print("The value of a is:",a)
add(a)
print("After the function call")
print("The value of a is:",a)

You might also like