Function 12

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Function Introduction

A function is a programming block of codes which is used to perform a single, related task.
It only runs when it is called. We can pass data, known as parameters, into a function. A
function can return data as a result.

Advantages of using functions:


1. Program development made easy and fast: Work can be divided among members
function thus implementation can be completed fast.

2. Program testing becomes easy: Easy to locate and isolate a faulty function.

3. Code sharing becomes possible: A function may be used later by many other programs
this means that a programmer can use function written by others.

4. Code re-usability increases: A function can be used to keep away from rewriting the same
block of codes which we are going use two or more locations in a program. This is especially
useful if the code involved is long or complicated.

5. Increases program readability: The length of the source program can be reduced by
using/calling functions at appropriate places so program become more readable.

6. Function facilitates procedural abstraction: Once a function is written, programmer


would have to know to invoke a function only, not it’s coding.

7. Functions facilitate the factoring of code: A function can be called in other function and
so on…

EX1. WAF to find sum of two no.

def abc(i , j):


s=i+j
print('Result=', s)

abc (2,3)
abc(11,33) Result= 5
abc(7,13) Result= 44
abc(100,22) Result= 20
abc(1000,500) Result= 122
Result= 1500
EX2. WAF to calculate the simple interest
def simpleInt(p,r,t):
si=(p*r*t)/100
return si

x=simpleInt(1000,2,5) Simple Interest= 100.0


print('Simple Interest=',x)
x=simpleInt(10000,5,3)
Simple Interest= 1500.0
print('Simple Interest=',x)

x=simpleInt(5000,6,3) Simple Interest= 900.0


print('Simple Interest=',x)
Pass some value and return result(non-void function):
def add(i,j):
s=i+j
return s
Pass some value and return nothing (void function):
def add(i,j):
s=i+j
print(‘Result=’, s)
Pass no value but return processed result (non-void function):
def add():
i=int(input('Enter the first value'))
j=int(input('Enter the second value'))
s=i+j
return(s)
Pass no value and return no result (void function):
def add():
i=int(input(‘Enter the first value’))
j=int(input(‘Enter the second value’))
s=i+j
print(‘Result=’, s)
void function do not return a value but they return a legal empty value of Python
i.e., None. Every void function return value None to its caller.

Ex3: WAF to calculate factorial of any given no.

def fact(N):
f=1
for i in range(N,0,-1):
f=f*i
return f

s=fact(4) Factorial= 24
print('Factorial=', s)
s=fact(5) Factorial= 120
print('Factorial=', s) Function calling
Factorial= 720
statement
s=fact(6)
print('Factorial=', s) Factorial= 5040
s=fact(7)
print('Factorial=', s)
Ex4: WAF to check whether a given no is prime or not.
def primeCheck(N):
f=1
for i in range(2,N):
if N%i==0:
f=0
break
if f==1:
print(N," is prime ")
else:
print(N," is not prime ")
primeCheck(5) 5 is prime
primeCheck(9) Function calling statement
9 is not prime
primeCheck(11) 11 is prime
primeCheck(25) 25 is not prime

Argument in python can be one of these value types:


 Literal
 Variable
 Expression
def mul(a,b):
m=a*b
print("Result: ", m)

mul(3,4) #Literal Result: 12

x=6
y=9 Result: 54
mul(x,y) #Variable
mul(x+2,y-3) #Expression Result: 48
Q. WAF to read three side and display volume

def calvol(a,b,c):
v=a*b*c
return v
Volume : 24
x=calvol(2,3,4)
print(‘ volume: ’, x) Function calling statement

Ex6: WAF to find whether a candidate is valid for vote.


def voter(age):
if age>=18:
print('valid for vote')
else:
print('Not valid for vote')

voter(4) Not valid for vote


voter(18) Function calling statement valid for vote
voter(24) valid for vote

There are two type of function call


mechanisms:
 Cal by Value
 Call by Reference
In call by value mechanism the called function makes separate copy of
passed value and then work with them, so original values remain
unchanged.

def abc(a,b): #a=5 b=12


a=a+2
b=b+5
print('Inside The Function: ', a,' ',b) #5 12
x=3
y=7
print('Befor Function Call: ',x,' ',y)
abc(x,y)
print('After Function Call: ',x,' ',y)
Output:
Befor Function Call: 3 7
Inside The Function: 5 12
After Function Call: 3 7
In call by reference mechanism the called function works with original
value passed to it, thus any changes made, takes place original values
only.
def abc(s):
s[0]=s[0]+1
s[1]=s[1]+2
print('Inside the function',s)

l=[10,20]
print('Before Function call',l)
abc(l)
print('After function call ',l)

Output:
Before Function call [10, 20]
Inside the function [11, 22]
After function call [11, 22]

In python, immutable types(number, string, tuple etc.) implement call


by value mechanism and
mutable types(list or dictionaries) implement call By Reference
mechanism.

Passing Parameters
1. Positional Argument
2. Default argument
3. Keyword argument
1. Positional/Required Argument:
def simpleInt(p,r,t):
si=(p*r*t)/100
return si

x=simpleInt(1000,2,5) Simple Interest=


print('Simple Interest=',x) 100.0

x=simpleInt(10000,5,3,4) #error
print('Simple Interest=',x)

TypeError: simpleInt() takes 3 positional arguments but 4


were given

x=simpleInt(5000,6) #error
print('Simple Interest=',x)

TypeError: simpleInt() missing 1 required positional


argument: 't'
2. Default Argument:

def simpleInt(p,r=5,t=2):
si=(p*r*t)/100
return si

x=simpleInt(5000)
print('Simple Interest=',x) Simple Interest= 500.0

x=simpleInt(5000,6)
print('Simple Interest=',x) Simple Interest= 600.0

x=simpleInt(1000,3,6)
print('Simple Interest=',x) Simple Interest= 180.0

x=simpleInt() #error
print('Simple Interest=',x)

TypeError: simpleInt() missing 1 required positional


argument: 'p'

x=simpleInt(10000,5,3,4) #error
print('Simple Interest=',x)
TypeError: simpleInt() takes from 1 to 3 positional
arguments but 4 were given
3. Keyword Argument(Named Argument):

def simpleInt(p,r,t):
si=(p*r*t)/100
return si

x=simpleInt(t=4,p=5000,r=2.5) Simple Interest= 500.0


print('Simple Interest=',x)

x=simpleInt(t=2,r=4,p=900)
print('Simple Interest=',x) Simple Interest= 72.0

x=simpleInt(r=3,p=1000,t=2)
Simple Interest= 60.0
print('Simple Interest=',x)
Using Multiple Argument Type Together

Returning multiple values


A function return multiple values in the form of tuple:
Ex1.
def squared(x,y,z):
return x*x, y*y, z*z

t=squared(3,4,5) (9, 16, 25)


print(t) Function return multiple value in the form of
tuple
Ex2.
def squared(x,y,z):
return x*x, y*y, z*z

t=squared(3,4,5)
a,b,c=t
print(a,b,c) 9 16 25

Ex3.

def squared(x,y,z):
tuple unpacking
return x*x, y*y, z*z

a,b,c = squared(3,4,5)
print(a,b,c) 9 16 25
SCOPE OF VARIABLE

The scope decide in which part(s) of the program, a particular piece


of code or data item would be known and can be accessed therein.
Or
Part(s) of program within which a variable is legal and accessible, is
called scope of the variable.

Global Scope: A variable declared in top level segment (__main__)


of a program is said to have a global scope and is usable inside the
whole program and all block (function, other block) contained within
the program.
X=5 Global Variable
def fun(a):
b=a+X
return b

y=input(‘Enter the number’)


z=y+func(x)
print(z)

Local Scope: A name(variable) declare in a function-body is said to


have local scope i.e. it can be used only within this function and the
other block contained under it.
def cal(x,y):
z=x+y
return z

num1=int(input('Enetr fisrt no'))


num2=int(input('Enetr second no'))
res= cal(num1,num2)
print('Sum=',res)
there are three variable num1,num2, res define in main program are
global variable and three variable x, y and z define in the function
cal() are local variable.
def calcsum(a,b,c):
print(q)
s=a+b+c
Enter the number1 :4
return s Enter the number2 :5
Enter the number3 :6
def average(x,y,z): 100
100
print(q) Average of three value 5.0
sm= calcsum(x,y,z)
return sm/3

q=100
num1=int(input('Enter the number1 :'))
num2=int(input('Enter the number2 :'))
num3=int(input('Enter the number3 :'))
av = average(num1,num2,num3)
print('Average of three value ',av)

Same variable name in local scope as well as in global scope

def cal():
r=100 2
print(r) 100
2
r=2
print(r)
cal()
print(r)

def cal():
global r 2
r=100
print(r)
100
100
r=2
print(r)
cal()
print(r)
Passing an Immutable Type Value to a Function

def fun(a):
a=a+10
print('Inside the fun ',a) Before the function
2
z=2
print('Befor the function ', z) Inside the fun 12
fun(z) After the function 2
print('After the function ', z)

z and a are refer to two different location

Passing an Mutable Type to a function-making changes in place

def fun(lst):
lst[0]+=10
lst.append(200) Befor the function call [4, 5]
print('Inside the fun ',lst)
Inside the fun [14, 5, 200]
q1=[33,44]
lst.extend(q1) Inside the fun [14, 5, 200, 33, 44]
print('Inside the fun ',lst)
After the function call [14, 5, 200, 33, 44]
L=[4,5]
print('Befor the function call ', L)
fun(L)
print('After the function call ', L)

L and lst are refer to same memory location

4 5 10 20
0 1 0 1

L lst q1
14 5 200 33 44
0 1 2 3 4

def fun(lst):
mst=[10,20] Befor the function call [4, 5]
lst=mst Inside the fun [15, 20, 200]
lst[0]+=5 After the function call [4, 5]
lst.append(200)
print('Inside the fun ',lst)

L=[4,5]
print('Befor the function call ', L)
fun(L)
print('After the function call ', L)

Inside the function lst refer to a new location which refer by mst
4 5 15 20
0 1 0 1

L lst mst

You might also like