Professional Documents
Culture Documents
Solution
Function header def processMumber (x)
Function call
processNumber (y)
Arguments
Parameters x
Function body x 72
return x+3
Main program y 54
res processMumber(y)
def calcsquare(x):
6. a power(x, 2)
return a
8
9 nS
10. result calcSquare(n) power(3, 3)
11. print(result)
Solution. Flow of execution for above code will be .
159105612367 10 1 -2-3- 10 11
The output produced by above code will be:
4Trace the flow of execution for follouwing programs
(4) def power(b, p): () . d e f Increment (x):
2 r-b*p 45
returnr X1
return
5 def calcSquare(a):
a power(a, 2) main
return a 7. y 3
8. print (y)
n 5 9. y increnent(y)
10 result calcSquare(n)
18. print (y)
11 print (result)
11. 7 7
)1. def increment (x) 12. print(q)
2. xX1 13. increnent(q)
3. 14. print(q)
4. main program 15. print(x)
5. x» 3 16. print (2)
6. print (x)
7. increment (x)
8. print(x)
Control didnot return to
Soiution call functon
statement (7) as nothina
(a) 15 »9->10>5 >6>1 >23>6>7->10-11 being returned by increment
b) 1 5 >6>7->1 >2 8
c) 17 8--9->12--3-49>10->11>12>131-2-3414->15l6
5What is the difference between the formal parameterTs and actual parameters? What are their alternot
ames? Als0, gioe a suitable Python code to illustrate both.
alternative
Sokution. Actual Parameter is a parameter, which is used infunction call statement to sendthe
from calling function to the called function. It is also known as Argument. value
Formal Parameter is a parameter, which is used in function header of the called
function to receive the
value from actual parameter. It is also known as Parameter. For
example,
def addEm(x, y, z):
print(x+y+2)
addEm(6, 16, 26)
In the above code, actual are
parameters 6, 16 and 26; and formal parameters arex, y and z.
6. Consider afunction with
following header
def =
info(object, spacing 10, collapse 1): =
f. info()
8. info(collapse e, obj3 )
e. info( spacing =
15, object obj4)
Sikution
Correct obj1 is for
and
positional parameter object;
collapse gets its default value of 1. spacing gets its default value of 10
Incorrect
Required
be missed.positional argument (object) missing; required arguments c a nbt
() Correct Required parameter object gets its value as
for skipped
argument collapse, default valueobj2; spacing gets value 12 an
Incorrect 1 is taken.
(d Same
-
nal
parameter object is given multiple values
one through
argument and one through
keyword(named) posito
(e) Correct Required parameter object gets its value as argument.
for skipped
argument spacingg default valueobj3; collapse gets value and
10 is taken.
Incorrect Required parameter objece's value cannot be
Incorrect Positional arguments should be before skipped.
(h Correct keyword arguments.
Required argument object gets its value
through a keyword argument.
Chapter 3:
WORKING WITH FUNCTIONS 141
What is the default return value for a function that does not return any value explicitly ?
7
(a) None (b) int (c) double (d) null
Solution. (a)
What will following code print ?
8.
def addEm(x, y, z):
print(x+y+z)
def prod (x, y, z) :
return x *y *z
b prod ( 2, 3, 6)
print(a, b)
Solution.
None 36
9. In the previous question's code, identify the void and non-void functions. The previous code stores the return
values of both void and non-void functions in variables. Why did Python not report an error when void
functions do not return a value?
Solution.
Void function addEm()
Non-void function prod()
In Python, void functions do not returm a value; rather they report the absence of returning value by
returning None, which is legal empty value in Python. Thus, variable a stores None and it is not any
error.
10. Consider below given function headers. Identify which of these will cause error and why ?
() def func(a = 1, b):
Solution. Function headers (, (i) and (iv) will cause error because non-default arguments
cannot follow default arguments.
Only function header (i) will not cause any error.
11. What is the difjerence bettoeen a local variable and a global variable ? Also, give a suitable Python code to
illustrate both.
Solution. The differernces between a local variable and global variable are as given below:
xCubed cude(x)
is", Cubed)
print(x,"cubat a fNtAn, but upon running the ouie .
w n tde
eiwes f rvw rariens thrh in main program
Folveng onde iedrmds a Put b d again
v p t shws shat the znaikues
ane sged ansde the savde)hune¥ion
y7
print(x-", x, "y-". y)
lebal Environment
switch(x, y)
print("x ", x, "y-", y) Local Environment
for switeh)
Sohution. The reason for un-retlected changes xG7 y
in the main program is that although both
main program and suritch( ) have variables
with same names ie. x and y, but their scopes
are different as shown in the adjacent figure.
The scope ofr and y of sweitcht )is local. Though they are swapped in the namespace of stoitch(bu
theirnamespace is removed as soon as control returns to main program. The global variables x anu
remain unchanged as suvitch( ) worked with a different copy of values not with original values.
The is
remedy of above problem that the suritch) gets to work with global variables so that
aremade in the global variables. This can be done with the help of global statement as shown below
chango
def switch(x, y):
global x, y
x, y=y,x
print("inside switch:", end = '')
print("x-", x, "y =", y)
X S
y 7
3. return
5. a 20
6. b 5
7. increase(b)
8. print(a)
Solution. The above code will
produce an error.
The reason being whenever we
created as a local variable.
assign something to a variable inside
Assignment creates a variable in Python.
a function, the variable is
Thus at line 2, when variable a is
incremented with the passed value x, Python tries to create a local
variable a by assigning to it the value of
variable a also appears in the expression the right hand side of
on
right hand side of assignment. But
undeclared so far in function. assignment,which results in error because a is
To assign some value to a global variable from within a
with the same name,
function, without creating a local variable
global statement can be used. So, if we add
global a
in the first line offunction body, the above error will be
variable a, rather will work with corrected. Python won't create a local
global variable a.
14. Which names are local, which are global and which are built-in in the following code fragment?
invaders = "Big names
pos 2008
level 1
defplay():
max_level = level+ 10
print(len (invaders ) == 0)
return max_level
res play()
print(res)
Solution. Global names invaders, pos, level, res
Local names max_level
Built in len
Find and write the output ofthefollowing python code : ICBSE Sample Paper 2019-20
a 10
def call():
global a
a 15
b 20
print(a)
call()
Solution. 15
Preudict the output of the following code fragment?
def func (message, num- 1):
p r i n t (message * num)
func Python')
func( Easy', 3)
Sohuthon
Python
EasyEasyEasy
17 Find and write the output of the following python code.
def fun(s):
k len(s)
print(m)
fun('schoo12gcom')
Solution
SCHOOLbbbbcoM
18 Find and write the
output of the following python code :
def
Change (P, Q= 30):
P P+Q
QP-Q
print (P,"#",0)
return (P)
R 150
S 100 150
R =Change(R,S)
print(R,"*",5)
S Change(s)
Soiution.
150 # 50
158# 100
100#70
19 Predict the output of
the following code fragment ?
def check(ni 1, n2 2): =
nl n1+ n2
Chopter3: WORKING WITH FUNCTIONS
n2 + 1
145
print(ni, n2)
check()
check(2, 1)
check(3)
Solution.
3 3
3 2
5 3
print (a)
Solution. The code will
print 1 to the console.
21. What will be the
output of following code?
def interest
(prnc, time =2 , rate =0.10)
return (prnc * time *
rate)
print(interest (6100, 1))
print(interest (5e00, rate 0.05)) =
Write a function that takes a positíve integer and returns the one's position digit of the integer
23
Solution.
def getones (num) :
# return the ones digit of the integer num
onesDigit num % 10
return onesDigit
Write a function that receives an octal number and prints the equivalent number in other number bases ie, n
24.
decimal, binary and hexadecimal equivalents.
Solution.
def oct2others( n) :
print("Passed octal number :", n)
numString str(n)
decNum= int( numString, 8)
print("Number in Decimal:", decNum)
print("Number in Binary: ", bin(decNum))
print("Number in Hexadecimal:", hex(decNum) )
GLOSSARY
Argumet A value provided to a function in the functon col statemant
Flow of execution The order of eecution of staternents during a progron run.
A name used inside a function to reler to the voalue which wos posed to it as on argumen
Parameter
Function Named subprogram thot acts on dota and often returns o value
Actual Argument Argument
Actual Parameter Argumen
Format Purameter Parameter
Pormal Argument Parameter essed
which a paricular piece of code or a dato value (e.g., voriable)
can be
occe
Scope Progran part(s) in