Function Type B Cs
Function Type B Cs
(b)
Answer =
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:-
21
2. onsider the following code and write the flow of execution for
this. Line numbers have been given for your reference.
Answer =
1-->5-->9-->10-->5-->6-->1-->2-->3-->7-->10-->11
print (x + y + z)
Answer =
Answer =
When function called then it will add all the argument and it will show
nothing.
(i)
num = 1
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
global num
num = 10
return num
print (num)
print(myfunc())
print(num)
(iv)
def display():
display()
print("there!")
Answer =
(i)
Output:-
1
1
1
>>>
(ii)
Output:-
1
10
1
(iii)
Output:-
1
10
10
(iv)
Output:-
Hello there!
6. Predict the output of the following code:
a = 10
y=5
def myfunc():
y=a
a=2
print("a+y =", a + y)
return a + y
print(myfunc())
Answer =
Output:-
y = 5 a = 10
Error
y=a
a=2
print("y =", y, "a =", a)
print("a+y =", a + y)
return a + y
in Function
myfunc()
return x + y + z
Answer =
Return is written before print , so when function will call then print
statement will not give any result.
When function called then it will add all the argument and it will show no
result.
Answer =
def fun():
return None
print (fun())
Output :-
None
>>>
9. Consider the code below and answer the questions that follow:
answer = number1*number2
return(answer)
output = multiply(5,5)
Answer =
(i)
Output:-
5 times 5 = 25
(ii)
return(answer)
output = multiply(5,5)
(i) When the code above is executed, what gets printed?
Answer =
(i)
It will show no result. Because return statement is written before print
statement.
(ii)
Variable is ‘answer’. And its value is 25.
(a)
print(output)
return (output)
(b)
define check()
i=3
answer = 1 + i ** 4 / N
Return answer
(c)
return beta(string)
return n
print(alpha("Valentine's Day") :)
print(alpha(n = 5, "Good-bye") :)
Answer =
(a)
def minus (total, decrement):
output = total - decrement
print(output)
return (output)
(b)
def check():
N = int(input ("Enter N: "))
i=3
answer = 1 + i ** 4 / N
return answer
(c)
Calling of function is in wrong way. If that is corrected then it will show no
error.
Then line 12 call length and create local environment within global
environment. Then length function return the values in global
environment. Then line 12 call mean and create local environment within
global environment. Then mean function call sum and create nested local
environment. Then sum function return values in local environment. Then
mean function call length and create nested local environment. Then
length function return values in local environment.
Answer =
1 --> 6 --> 9 --> 12 --> 1 --> 2 --> 3 --> 4 --> 12 --> 6 --> 7 --> 12 --
> 9 --> 10 --> 1 --> 2 --> 3 --> 4 --> 10 --> 6 --> 7 --> 10 --> 12
14. 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
Answer =
Answer =
def fun(x):
print (x ** 2)
fun(4)
fun(8)
fun(6)
fun(2+1)
fun(4-3*2)
fun(-3-2)
Answer =
Program –
def fun(x): #1
return (x ** 2) #2
print (fun(4)) #3
print (fun(6)) #4
print (fun(2+1)) #5
print (fun(4-3*2)) #6
print (fun(3-2)) #7
Flow of execution:
1 --> 3 --> 1 --> 2 --> 3 --> 4 --> 1 --> 2 --> 4 --> 5 --> 1 --> 2 --> 5
--> 6 --> 1 --> 2 --> 6 --> 7 --> 1 --> 2 --> 7
17. 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])
print(L)
print(L[3] == m4)
Answer =
(i)
Output:-
(ii)
Output: -