Function 12
Function 12
Functions
Introduction
Function
It is a subprogram that acts on data and often return
value(s).
Understanding function
Let us consider polynomial function f(x)=2x2
for x =1 it gives 2
for x=3 it gives 18
Where x is argument and f() is function
Advantages of using function
1. To make program handling easier i.e. only a small part of
program dealt with at a time.
2. To reduce the program size.
3. Make program more readable and understandable.
4. A function once defined can be called/invoked as many
time as needed by using its names without having to
rewrite code.
5. Reusable functions can be put in a library modules. These
module can be imported and used when needed in other
programs.
Python Function Types
Default arguments ar
l gu Ke
a
n ts m yw
o ar en or
s iti en d gu ts d
Po gum uire rs) m (na
en m
ar eq ete ts) ed
(R am
a r
P
Positional / Required arguments
When the function call statements must match the number and order of
arguments as defined in the function definition, this is called the positional
argument matching.
Example :
def sum(a,b,c) :
d= a+b+c
return d
Then the positional function call for the above function
sum(x,y,z) or sum(5,x,y) or sum(2,3,6)
Thus through such function calls,
* The arguments must be provided for all parameter (required)
* The values of arguments are matched with parameters, positional (order) wise
(positional)
Default Arguments
• A function having default value in the function call statements. This is
useful in case a matching argument is not passed in the function call
statement.
• In a function header, any parameter can’t have a default value unless all
parameters appearing on its right have their default values.
• Example
def interest(principal, time, rate=2)
.
.
.
Si_int =interest(4500,5) # third argument is default value
in function definition.
Identify following headers with default values are legal or illegal
Global Local
Scope Scope
Global Scope:- A name declared in top level segment (_main_) of
a program and it is usable inside the whole program and all
blocks contained within the program.
• Local Scope:- A name declare in a function body is said to have local scope i.e.
it can be used only within that function/block
• Example
d=5 # global variable
def calsum(a,b,c) : # local variable
s=a+b+c
return s
def average (x,y,z): # local variable
sm=calsum(x,y,z)
return sm/3
num1=int(input(“Number 1”)) # global variable
num2=int(input(“Number2”)) # global variable
num3=int(input(“Number3”)) # global variable
Print(“Average of these number is”, average(num1, num2, num3))
Life time of a Variable
• The time for which a variable name remain in the
memory is called for global variable life time is entire
program run and for local variables is their function run.
Name Resolution (Resolving Scope of a Name)
• Name resolution rule is also known LEGB rule
• Local environment :- It checks within its local environment if it has a
variable with the same name. if not, then it moves to step (ii)
• Enclosing environment :- Python now checks the enclosing environment
if whether there is a variable with same name. If not then it moves to
step (iii)
• Global environment:- Python now checks the Global environment
whether there is a variable with the same name. if not then it moves to
step (iv)
• Built in environment :- Python checks its built in environment that
contain all built in variables and function of python if there is a variable
with the same name; if yes, python uses its value.
To use Global variable inside the local scope
• The global statement is a declaration which hold for the entire current code block. To tell a
function that for a particular name do not create a local variable but use global variable instead.
Syntax
global <variable_name>
example :
def state1 () :
global tigers
tigers =15
print (tigers)
tigers =95
print(tigers)
state1()
print(tigers)
Note :- If same variable name in local scope as well as in global scope, then python uses
local scope instead of global variable.
Mutable/Immutable properties of passed data objects
• If a variable is referring to an immutable type then any change in its value will also change the memory
address it is referring to. Example
def muFunc1(a):
print (“\t Inside myFunc1()”)
print (“\t Value received in ‘a’ as, a)
a=a+2
print (“\t Value of ‘a’ now changes to “,a)
print (“\t returning from myFunc1()”)
num =3
print (“ Calling myFunc1() by passing ‘num’ with value”, num)
myFunc1(num)
print(“Back form myFunc1(). Value of ‘num’ is “, num)
Output : if num=3
Inside myFunc1()
Value received in ‘a’ as 3
Value of ‘a’ now changes to 5 # Value not changes form 3 to 5 inside function
Returning form myFunc1()
If a variable is referring to mutable type then any change in the value of mutable type will not change the memory address of the
variable . Example :
def muFunc2(myList):
print (“\t Inside called function now ”)
print (“\t List received :”, myList)
myList.append (2)
myList.extend([5,1])
print (“\t List after adding some elements: “, myList)
myList.remove(5)
print (“\t List within called function, after all change :”, myList)
return
List1=[1]
print (“List before function call :” List1)
myFunc2(List1)
Print(“\t List after function call :”, List1)
Output:
List before function call : [1]
Inside called function now
List received : [1]
List after adding some elements : [1,2,5,1]
List within called function, after all changes : [1,2,1]
List after function call : [1,2,1] # The value got changed from [1] to [1,2,1] inside function