Functions (ch3)
Functions (ch3)
Introduction
Function is a subprogram that acts on data and
often returns a value.
2
for x=1, result is 2
for x=2, result is 8
for x=3, result is 18
for x=4, result is 32
F(x)=2
Notataion f(x) is termed as function,
Where x is the argument.
def calc(x):
r=2*x**2 Statement to return computed
body
result
retrun r
a=int(input(“enter a number”))
print(cal(a))
Function call
Calling/invoking /using a function
1.Passing literal as argument in function call.
cube(4)
2.Passing variable as argument in function call.
num=10
cube(num)
3.Taking input and passing the input as argument.
num=int(input(“enter a number”))
cube(num)
4.Using function call inside another stmt.
print(cube(3))
5.Using function call inside expression.
doubleofcube=2*cube(6)
Python function types
Built-in functions
int(), type(), len(), input()
def greet():
print(“good mrng”)
parameters
def sum(x,y):
s=x+y Function header
return s
Sample code 1 Sample code 3
s=n*1+n*2+n*3 s=n*1+n*2+n*3
return s print(s)
Sample code 2
def areaofsquare(a):
return a*a
Sample code 3
def quote():
print(“Do not expect anything from anyone”)
Structure of a python program
By default, python names the segment with top-level statements
(main program) as __main__
def function():
:
def function2():
:
#top level stmt
stmt 1
stmt2
def greet():
print(“hi there”)
print(“at the top-level”)
print(“inside”,__name__)
Flow of execution in a function call
Flow of execution refers to the order in which statements are executed
during a program run.
2,6,7,8,2,3,4,8,9
Arguments and parameters
Arguments : python refers to the values being passed as arguments
sum(5,6) (Actual parameter / actual argument)
def one(a+1,b)
c=a+b
retrun(c)
Today’s Program:(04.03.23)
1.Write a function that takes amount-in-dollars-to-rupee conversion
price, it then returns the amount converted to rupees. Create the
function in both void and non-void forms.
2. Write a function to calculate volume of a box with appropriate default
values for its parameters. Your function should have the following input
parameters.
(a)Length of box
(b)Width of box
(c)height of box
Passing Parameters
Default arguments
check (x,y,z)
Function call
check(2,x,y)
check(2,5,7)
Default arguments
def legal
interest(prin=200,time=2,rate=0.10):
Keyword (or named ) arguments
interest (prin=2000,time=2,rate=0.10)
interest(time=4,prin=2600,rate=0.90) Function call
interest(time=2,rate=0.12,prin=2000)
Using Multiple argument types together
Function call
interest(5000,time=5)
Rules:
Positional should be in first and then keyword argument.
Cannot specify a value foe an argument more than one
time.
def interest(prin,cc,time=2,rate=0.09):
retrun prin*time*rate
Function call
interest(prin=3000,cc=5) Legal
interest(rate=0.12,prin=5000,cc=4) Legal
interest(cc=4,rate=0.12.prin=5000) Legal
interest(5000,3,rate=0.05) Legal
interest(rate=0.05,5000,3) Illegal
interest(5000,prin=300,cc=2) Illegal
interest(5000,principal=300,cc=2) Illegal
interest(500,time=2,rate=0.05) illegal
Returning values from functions
Literal
A variable
Expression
return 5
return 6=4
return a
return a**3
return (a+8**2)/b
return a+b/c
add_result=sum(a,b)
print(sum(3,4))
sum(4,5)>6
def square(x,y,z):
return x*x,y*y,z*z
t=square(2,3,4)
Print(t) #t is tuple
def square(x,y,z):
return x*x,y*y,z*z
a,b,c=square(2,3,4)
Print(a,b,c) #a,b,c normal variables created based
on the values
Scope of variables
Parts of program within which a name is legal and
accessible , is called scope of the name.
LEGB
L- Local Environment
E- Enclosing Environment
G- Global Environment
B- Built-in Environment
Case 1: variable in global but not in local
def calcsum(x,y):
s=x+y
print(num1)
return s
num1=int(input(“enter first number:”))
num2=int(input(“enter second number:”))
print(“sum is”,calcsum(num1,num2))
Case 2: variable neither in local and
global
def greet()
print(“hello”,name)
greet()
error
Case 3:Same variable in local and global
def state():
tigers=15
print(tigers)
Output:
tigers=95 95
Print (tigers) 15
State() 95
Print(tigers)
What if you want the global variable
inside local scope
Global keyword is used
def state():
global tigers
tigers=15 Output:
print(tigers) 95
tigers=95 15
Print(tigers) 15
State()
Print(tigers)
Find output
def update(x=10):
x+=15
print(‘x=‘,x) Output:
x=20 x=25
update() x=20
print(‘x=‘,x)
Find the output
def addem(x,y,z):
print(x,y,z)
def prod(x,y,z): Output:
return x*y*z 6 16 26
a=addem(6,16,26) None 36
b=prod(2,3,6)
Print(a,b)
Write a function that takes a positive
integer and returns the one’s position
digit of the integer.
Mutable /Immutable Properties of
passed data objects
Sample code 1 Calling myFunc1() by passing ‘num’ with value 3
Inside myFunc1()
Passing an Immutable type value to a function.
Value received in ‘a’ as 3
1.def myfunc1(a): Value of ‘a’ now changes to 8
returning from myFunc1()
2. print(“\t Inside myFunc1()”) Back from myFunc1().Value of ‘num’ is 3
1.def myfunc4(myList):
2. print(“\n Inside Called function now”)
List before function call:[1,4]
inside called function now
3. print(“\t List received:”,myList) List received: [1,4]
4. new=[3,5] List within called function, after changes: [3,5
List after function call:[1,4]
5. myList=new
6. myList.append(6)
7. print(“\t List within called function,after changes”,myList)
8. return
9.List1=[1,4]
10.print(“List before function call:”,List1)
11.myFunc4(List1)
12.print(“\n List after function call:”,List1)
Mutable Argument (say A) assigned to mutable parameter (say p)