Functions - Type B Questions

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

Working with Functions

Type B Questions and Answers


( Write and practice in Notebook )
Q.1 . What are the errors in following codes? Correct the code and predict output:
(a)
total = 0 ;
def sum( arg1, arg2 ):
total = arg1 + arg2 ;
print("Total :", total)
return total ;
sum( 10, 20 ) ;
print("Total :", total)

(b)

def Tot (Number) #Method to find Total


Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])

Answer =
(i) In line 3 & 4 there should be double indentation.

total = 0
def sum( arg1, arg2 ):
total = arg1 + arg2
print("Total :", total)
return total
sum( 10, 20 )
print("Total :", total)

Output:-
Total : 30
Total : 0

(ii) In line 5 "RETURN" should be in small words and also there is only one indentation.

def Tot (Number): #Method to find Total


Sum = 0
for C in range (1, Number + 1):
Sum += C
return Sum
print (Tot(3)) #Function Calls
print (Tot(6))

Output:-

6
21

Q. Consider the following code and write the flow of execution for this. Line numbers
have been given for your reference.
def power (b, p): #1
y = b ** p #2
return y #3
#4
def calcSquare(x): #5
a = power (x, 2) #6
return a #7
#8
n=5 #9
result = calcSquare(n) # 10
print (result) # 11
Answer :-

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

Output :-

25

Q. What will the following function return?

def addEm(x, y, z):


print (x + y + z)

Answer =

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.


Q. What will the following function print when called?

def addEm(x, y, z):


return x + y + z
print (x+ y + z)

Answer =

When function called then it will add all the argument and it will show nothing.

Because return keyword will work before print statement.

Q. What will be the output of following programs?

(i)
num = 1
def myfunc ():
return num
print(num)
print(myfunc())
print(num)

(ii)
num = 1
def myfunc():
num = 10
return num
print (num)
print(myfunc())
print(num)

(iii)
num = 1
def myfunc ():
global num
num = 10
return num
print (num)
print(myfunc())
print(num)

(iv)
def display():
print("Hello", end = ' ')
display()
print("there!")

Answer =

(i)
Output:-

1
1
1
>>>

(ii)
Output:-

1
10
1

(iii)
Output:-

1
10
10

(iv)
Output:-

Hello there!

You might also like