FUNCTIONS
FUNCTIONS
1. Positional Arguments.
def fun(pri,rate,time):
si=pri*rate*time/100
return si
Choose the right Caller
fun(45000,4,2) #True
fun(45000,4) #Wrong
fun(45000) #Wrong
fun(45000,4,2,7)#Wrong
2. Default Argument.
NOTE:- in default argument if left side variable is default
than all variable should be default.
def fun(pri,rate=7,time=2):
si=pri*rate*time/100
return si
Choose the right Caller
fun(45000,4,6)#True
fun(45000,4) #True
fun(45000)#True
fun(45000,4,2,7)#Wrong
3. Keyword[Named] Arguments:- python offers a way of
writing function calls where you can write any argument
in any order provided you name the arguments when
calling the function.
def fun(pri,rate,time):
print("Principal : ",pri)
print("Rate : ",rate)
print("Time : ",time)
fun(rate=4,time=2,pri=45000)
Rules for combining all three types of arguments.
a)an argument list must first contain
positional(required) arguments followed by keyword
argument.
b)keywords arguments should be taken from
required argument.
c)you can not specify a value for an argument more
than once.
Example:-
def interest(prin,cc,time=2,rate=0.02):
si=prin*rate*time/100
return si
Choose the right Caller
interest(prin=3000 , cc=5) #TRUE
interest(rate=0.02 , prin=5000 , cc=4) #TRUE
interest(cc=4 , rate=0.12 , prin=8000) #TRUE
interest(5000 , 3 , rate=0.05) #TRUE
interest(rate=0.08,5000,4) #WRONG
[keywords argument before positional argument]
interest(5000 , prin=300 , cc=2) #WRONG
[Morethan one value can not be provided]
interest(5000 , principal=300 , cc=2) #WRONG
interest(500 , time=2 , rate=0.05) #WRONG
SCOPE:- Parts of program within which a name is legel
and accesible, is called scope of the name.
1. Local SCOPE
def fun():
x=200 #Local Scope
print("x : ",x)
fun()
print("x : ",x) #Error
2. Global SCOPE:- A name 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
blocks.
def fun():
print("x : ",x)
x=200 #Global Scope
fun() #Caller
print("x : ",x) #No Error
NOTE- same variable name in local scope as well as in
global scope.
Ex-1
def state1():
tigers=15 #[Local Scope]
print(tigers)
tigers = 95 #[Global Scope]
print(tigers)
state1()
print(tigers)
Output:
95
15
95
Ex-2
def state1():
global tigers
tigers=15 #[Local Scope]
print(tigers)
tigers = 95 #[Global Scope]
print(tigers)
state1()
print(tigers)
Output:
95
15
15