Functions

You might also like

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

Scope of Variables in Python

A variable isn’t visible everywhere and alive every time.

1. Scope
A variable’s scope tells us where in the program it is visible. A variable may have local or global
scope.

 Local Scope- A variable that’s declared inside a function has a local scope. In other words, it
is local to that function.

def fun(): def fun(): def fun(x):


s = "I love India!" s = "I love India!" a=10 # local
#local variable #local variable x=x+10
print(s) print(s) print(a,x)
fun() s = "I love World!" a,x= 1,2
print(s) fun() fun(x)
print(s) print(a,x)
output is :- output is :- output is :-
I love India I love India (10, 12)
Name error s not defined (1, 2)
I love World

Global Scope- When you declare a variable outside python function, or anything else, it has
global scope. It means that it is visible everywhere within the program.
However, you can’t change its value from inside a local scope(here, inside a function). To do so,
you must declare it global inside the function, using the ‘global’ keyword.

a=10 #global def fun(x):


def fun(x): global a
x=x+10 a=10
print(a,x) x=x+10
x= 1 print(a,x)
fun(x) a,x= 1,2
print(a,x) fun(x)
print(a,x)
output is :-
(10, 11) output is :-
(10, 1) (10, 12)
(10, 2)
As we know in python, every thing is as object. If the value of the object is changed, it is
called mutable, while if the value of object cannot change, it is called immutable.Example of
immutable objects are int, float, tuple, bool and str. Example of immutable are list, dict and set.

Passing int variable to function Passing string to function


def change(a): def count(str):
a=a+10 count = 0
q=10 for ch in str:
change(q) if ch in "aeiouAEIOU":
print (q) count +=1
output is :-10 return count
str="computer"
print (count(str))
output is :- 3

Passing tuple as Passing list/ array as Passing dictionary as argument


arguement arguement
def change(marks): def change(marks): def change(d1):
for i in for i in range(len(marks)): d1['roll']=1
range(len(marks)): marks[i]+=10 d1['sec']='a'
print (marks[i]), li=[10,20,30,40] d1={ "name": "amit", "class":12}
li=(10,20,30,40) change(li) change(d1)
change(li) print (li) print (d1)
output is :- 10 20 30 40 output is :- [20, 30, 40, 50] output is :-
{'roll': 1, 'sec': 'a', 'name': 'amit',
'class': 12}

NOTE: The changes reflect back after calling function as for mutable data types by default (call
by refernce method). The changes does not reflect back after calling function as for immutable
data types by default (call by value method)

VALUE RETURNING FUNCTION

Single Value returning function Multiple Values returning function


def disp(A, B):// formal parameters def cal(a, b):
return (A+B) c=a+b
d=a-b
X,Y=10,20 e=a*b
C=disp(X,Y) f=a/b
print ( C) g=a%b
output is:- 30 return c,d,e,f,g

q=cal(10,20)
print (q)
output is:- (30, -10, 200, 0, 10)

In Python, comma-separated values are


considered tuples without parentheses

Note :- If function does not have return statement in it then it return None
Passing Parameters to function

Required/Positional Keyword Arguments (we Default Variable


arguments (have to pass in are not required to argument(always length (any
the same order as used in remember the position of should be last no. of
function definition) argument) argument) arguments
can be
passed)
def cal(a, b, ch): def cal(a, b, ch): def cal(a, b, ch='+'): def sum(*
if ch=='+': if ch=='+': if ch=='+': var):
print (a+b) print (a+b) print (a+b) s=0
elif ch=='-': elif ch=='-': elif ch=='-': for i in var:
print (a-b) print (a-b) print (a-b) s=s+i
elif ch=='*': elif ch=='*': elif ch=='*': print (s)
print (a*b) print (a*b) print (a*b) sum(10)
elif ch=='/': elif ch=='/': elif ch=='/': sum(10,20)
print (a/b) print (a/b) print (a/b) output is:-
elif ch=='%': elif ch=='%': elif ch=='%': 10
print (a%b) print (a%b) print (a%b) 30
cal(4,5)
cal(4,5,’+’) cal(4,5,’*’)
x,y=20,30 cal(b=10,ch='%', a=30) output is:-
c='*' #keyword arguments 9
cal(x,y,c) output is:- 20
cal(c,x,y)#wrong (no 0
output)
output is :-
9
600

You might also like