12 CS - Functions Questions
12 CS - Functions Questions
A name declared in top level segment of a program is said to have a global scope and is usable inside the whole
program and all blocks (functions, other blocks) contained within the program.
Local Scope
A name declared in a function body is said to have a local scope and is usable within that function and the other blocks
contained under it. The formal arguments also have local scope.
def sum():
c=a+b
print (c)
sum()
def sum(a,b):
c=a+b
print (c)
sum(a,b)
def sum(a,b):
c=a+b
return c
x=sum(a,b)
print(x)
def update(x=10):
x+=15
print(‘x=’,x)
x=20
update()
print(‘x=’,x)
OUTPUT
x=25
x=20
def changeval(m,n):
for i in range(n):
if m[i]%5==0:
m[i]//=5
if m[i]%3==0:
m[i]//=3
l=[25,8,75,12]
changeval(l,4)
for i in l:
print(i, end=’#’)
25 8 75 12
0 1 2 3
5 8 5 4
OUTPUT
5#8#5#4#
5854#..................X
5#8#5#4……………X
a=10
def call():
global a
a=15
b=20
print(a)
call()
OUTPUT
15
print(message*num)
func(‘Python’)
func(‘Easy’,3)
OUTPUT
Python
EasyEasyEasy
def fun(s):
k=len(s) 11
m=” ”
if (s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+’bb’
print(m) m
fun(‘school2@com)
OUTPUT
SCHOOLbbbbCOM
P=P+Q
Q=P-Q
print(P,”#”,Q)
return(P)
R=150
S=100
R=Change(R,S)
print(R,”#”,S)
S=Change(S)
def check(n1=1,n2=2):
n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)
working
OUTPUT
33
32
53
a=1
def f():
a=10
print(a)
Output
1. call(R,S)
R=200 S=100
P=200 Q=100
P= P+Q=300 Q=P-Q=300-100=200
300@200
R=300 S=100
300@100
2. call(S)
S=100
P=100 Q=20
P= P+Q=120 Q=P-Q=120-20=100
120@100
R=300 S=120
300@120
OUTPUT
300@200
300@100
120@100
300@120
WORKING
print(interest(6100,1)) 6100x1x0.10=610.0
print(interest(5000,r=0.05)) 5000x2x0.05=500.0
print(interest(5000,3,0.12)) 5000x3x0.12=1800.0
print(interest(t=4,p=5000)) 5000x0.10x4=2000.0
OUTPUT
610.0
500.0
1800.0
2000.0
11. Find the error. Correct the code and predict the output:
total=0;
def sum(arg1,arg2):
total=arg1+arg2;
print(“Total :”,total)
return total;
sum(10,20);
print(“Total : “,total)
CORRECT CODE
total=0 //no semi colon should be there
def sum(arg1,arg2): //wrong indentation
total=arg1+arg2 // no semi colon should be there
print(“Total :”,total)
return total //wrong indentation, no semi colon should be there
total=sum(10,20) // no semi colon should be there, variable total should be
there to receive the value from the function
print(“Total : “,total)
OUTPUT
Total :30
Total : 30
12. Find the error. Correct the code and predict the output:
def tot(n)
s=0
for i in range(1,n+1):
s=s+i
RETURN s
print(tot[3])
print(tot[6])
CORRECT CODE
def tot(n): //colon missing
s=0
for i in range(1,n+1):
s=s+i
return s //return should be in small letters
print(tot(3)) //instead of list brackets, it should be method brackets
OUTPUT
6
21