Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Computer science

Holiday homework

Worksheet

Questions

1) Define function ?
2) What is the difference between local variable and global variable ?
3) What is an argument? Give an example.
4) What is Python module? What is its significance?
5) What is the utility of: -

(I) default arguments

(II) Keyword arguments?

6) What is the utility of the built-in function help()?


7) Define flow of execution. What does it do with functions?
8) Write a program to find the greatest common divisor between two numbers.
9) Write a Python function that takes a number as a parameter and checks whether the number is
prime or not.
10) What is a recursive function? Write one advantage of recursive functions.

Some program based questions

11). What will the following function return?

def addEm(x, y, z):

print (x + y + z)

12). Trace the following code and predict the output produced by it

1. def power (b, p):

2. y = b ** P

3. return y

4.

5. diff calcsquare (x) :

6. a = power (x, 2)

7. return a

8.
9. n = 5

10. result = calcsquare (n) + power (3,3)

11. print (result)

13) What is the output of following code fragments?

(i)

def increment(n):

n. append([4])

return n

L = [1, 2, 3]

M = increment(L)

print(L, M)

(ii)

def increment(n):

n. append([49])

return n[0], n[1], n[2], n[3]

L = [23, 35, 47]

mi, m2, m3, m4 = increment (L)

print(L)

print(mi, m2, m3, m4)

print(L[3] == m4)

14) Q .In the following code, which variables are in the same scope?

def func1():

a=1

b=2

def func2():

c=3

d=4

e=5
15) Q. From the program code given below, identify the parts mentioned below:

def processNumber(x):

x = 72

return x + 3

y = 54

res = process Number (y)

Identify these parts: function header, function call, arguments, parameters, function body, main program

16) Q. What will following code print?

def addEm(x, y, z):

print (x + y + z)

def prod (x, y, z):

return x * y * z

a = addEm (6, 16, 26)

b = prod (2, 3, 6)

print(a, b)

Why did Python not report an error when void functions do not return a value?

17) Q. Consider below given function headers. Identify which of these will cause error and why?

(i) def func(a = 1, b):

(ii) def func(a = 1, b, c = 2):

(iii) def func(a = 1, b = 1, c = 2):

(iv) def func (a = 1, b = 1, c = 2, d):

18) Q. Following code intends to add a given value to global variable a. What will the following code
produce ?

def increase(x): #1

a = a + x #2

return #3

#4

a = 20 #5

b = 5 #6
increase(b) #7

print(a) #8

19) Q. Find and write the output of the following python code:

a = 10

def call():

global a

a = 15

b = 20

print(a)

call()

20) Q. Find and write the output of the following python code:

def Change (P, Q = 30):

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)
Solutions

Ans1) A function in Python is a named block of statements within a program.

Ans2) Local variable:-


• It is a variable which is declared within a function or within a block.
• It is accessible only within a function/block in which it is declared.

Global variable: -
• It is variable which is declared outside all the functions.
• It is accessible throughout the program.

Ans3) An argument is data passed to a function through function call statement.

For example :- In the statement print(math.sqrt(25)) the integer 25 is an


argument.

Ans4) A "module" is a chunk of Python code that exists in its own (.py) file and is
intended to be used by Python code outside itself.

Modules allow one to bundle together code in a form in which it can easily be
used later. The Modules can be "imported" in other programs so the functions
and other definitions in imported modules become available to code that
imports them.

Ans5) (i)
A parameter having a default value in function header becomes optional in
function call. Functions call may or may not have value for it.

(ii)
Keyword arguments are the named arguments with assigned values being
passed in the function call statement.
Ans6) Python's built-in function help() is very useful. When it is provided with a
program name or a module-name or a function-name as an argument, it displays
the documentation of the argument as help. It also displays the doc-string within
its passed-argument's definition.

For example:

>>> help (math)

• It will display the documentation related to module math.

• It can even take function name as argument.

For example:

>>>help (math.sqrt())

• The above code will list the documentation of math.sqrt() function only.

Ans7) The flow of execution refers to the order in which statement are executed
during a program run.

It do nothing with function

Ans8)

def great(a , b) :
If b == 0 :
return a
else :
return great (b, a % b)

num1 = int (input("Enter the first number :- "))


num2 = int (input ("Enter the second number :- "))
print ("Greatest common divisor of ", num1 ,'&', num2, 'is'
,great(num1 , num2))

Output :-
Enter the first number :- 257
Enter the second number :- 638
Greatest common divisor of 257 & 638 is 1
>>>

Enter the first number :- 24

Enter the second number :- 240

Greatest common divisor of 24 & 240 is 24

Ans9)

Ans9) def prime(a):


for i in range (2,a) :
if a % i == 0 :
return "This is not prime number"
return "Prime number"

a = int(input("Enter a number : "))


print(prime(a))
Ans10) A function calling itself is known as recursive function.

Advantage: -
• Recursion makes the code short and simple.

Program based answers

Ans11) It is a void function.

So, it will return None.

If we pass values of x,y, and z in function. It will give an output that is x + y + z.


Ans12) Flow of execution for the above code will be:

1->5 ->9 ->10 ->6->2 ->3-7->2->3 ->11

The output produced by the above code will be:


52

Ans13)

(i)
def increment(n):
n. append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)

(ii)
def increment(n):
n. append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
mi, m2, m3, m4 = increment (L)
print(L)
print(mi, m2, m3, m4)
print(L[3] == m4)

(i)
Output:-

[1, 2, 3, [4]] [1, 2, 3, [4]]

(ii)
Output: -

[23, 35, 47, [49]]


23 35 47 [49]
True

Ans14)Here a, b, c, d are in same scope i.e. local scope.


Ans15) Function header :- def processNumber (x):

Function call :- processNumber(y)

Arguments :- y

Parameters :- x

Function body :-
x = 72
return x + 3

Main program :-
y = 54
res = processNumber (y)

Ans16) Output :-

48
None 36
>>>

Reason :-

Void function :- addEm()

Non-void function :- prod()

In Python, void functions do not return 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

Ans17) Function headers (i), (ii) and (iv) will cause error because non-default
arguments cannot follow default arguments.
Only function header (ii) will not cause any error.

Ans18) The above code will produce an error.

The reason being whenever we assign something to a variable inside a function,


the variable is created as a local variable. Assignment creates a variable in
Python.

Thus in 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 the expression on
the right-hand side of the assignment. But variable a also appears on the right-
hand side of the assignment, which results in an error because a is undeclared
so far in function.

To assign some value to a global variable from within a function, without creating
a local variable with the same name, the global statement can be used. So, if we
add

global a

In the first line of the function body, the above error will be corrected. Python
won't create a local variable a, rather will work with global variable a.

Ans19) Output :-

15

Ans20) Output :-

250 # 150
250 # 100
130 # 100

You might also like