0% found this document useful (0 votes)
50 views

Function Type B Cs

The document provides examples of Python functions and code snippets with explanations. It discusses function definitions, scopes, parameters, return values, and flow of execution. Examples include functions that take parameters, return values, print outputs, and call other defined functions. Explanations are provided for variable scopes, return statements, print outputs, and errors in code snippets.

Uploaded by

mjmukthar96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Function Type B Cs

The document provides examples of Python functions and code snippets with explanations. It discusses function definitions, scopes, parameters, return values, and flow of execution. Examples include functions that take parameters, return values, print outputs, and call other defined functions. Explanations are provided for variable scopes, return statements, print outputs, and errors in code snippets.

Uploaded by

mjmukthar96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Grade 12 :CS

CHAPTER 3 : Working with Functions


TYPE B:
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:-

21

2. onsider 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-->7-->10-->11

3. What will the following function return?

def addEm(x, y, z):

print (x + y + z)
Answer =

When arguments are passed in addEm(x, y, z) then it add all the


arguments and print the result.

IT is non void Function.

4. 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.

5. 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!
6. Predict the output of the following code:

a = 10

y=5

def myfunc():

y=a

a=2

print("y =", y, "a =", a)

print("a+y =", a + y)

return a + y

print("y =", y, "a =", a)

print(myfunc())

print("y =", y, "a =", a)

Answer =

Output:-

y = 5 a = 10

Error

Explanation :- Because at first y and a are global variable so we


can not use y,a (variable) directly in function.
If we want to use "y" and "a" in function, we have to write
global y
global a
before

y=a
a=2
print("y =", y, "a =", a)
print("a+y =", a + y)
return a + y

in Function
myfunc()

7. What is wrong with the following function definition?

def addEm(x, y, z):

return x + y + z

print("the answer is", 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.

8. Write a function namely fun that takes no parameters and


always returns None.

Answer =

def fun():
return None

print (fun())
Output :-

None

>>>

9. Consider the code below and answer the questions that follow:

def multiply(number1, number2) :

answer = number1*number2

print(number1, 'times', number2, '=', answer)

return(answer)

output = multiply(5,5)

(i) When the code above is executed, what prints out?

(ii) What is variable output equal to after the code is executed?

Answer =

(i)

Output:-

5 times 5 = 25

(ii)

Variable is ‘answer’. And its value is 25.


10. Consider the code below and answer the questions that follow:

def multiply(number1, number2):

answer = number1 * number2

return(answer)

print(number, 'times', number2, '=', answer)

output = multiply(5,5)
(i) When the code above is executed, what gets printed?

(ii) What is variable output equal to after the code is executed?

Answer =

(i)
It will show no result. Because return statement is written before print
statement.

(ii)
Variable is ‘answer’. And its value is 25.

11. Find the errors in code given below:

(a)

def minus (total, decrement)

output = total - decrement

print(output)

return (output)

(b)

define check()

N = input ("Enter N: ")

i=3

answer = 1 + i ** 4 / N

Return answer

(c)

def alpha (n, string ='xyz', k = 10) :

return beta(string)

return n

def beta (string)


return string == str(n)

print(alpha("Valentine's Day") :)

print(beta (string = 'true' ))

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.

12. Draw the entire environment, including all user-defined


variables at the time line 10 is being executed.

def sum(a, b, c, d): #1


result = 0 #2
result = result + a + b + c + d #3
return result #4
#5
def length(): #6
return 4 #7
#8
def mean(a, b, c, d): #9
return float (sum (a, b, c, d))/length() #10
#11
print (sum(a, b, c, d), length(), mean(a, b, c, d)) #12
Answer =

When line 10 executes then it must be call from line 12.

At first line 12 is created in global environment. Then it call sum and


create local environment within global environment. Then sum function
return the values in global environment.

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.

Then mean function return values in line 12 in global environment.


13. Draw flow of execution for above program.

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 =

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


15. Write a program with a function that takes an integer and
prints the number that follows after it: Call the function with these
arguments:
4, 6, 8, 2+1, 4 - 3 * 2, -3 - 2

Answer =

def fun(x):
print (x ** 2)

fun(4)
fun(8)
fun(6)
fun(2+1)
fun(4-3*2)
fun(-3-2)

16. Write a program with non-void version of above function and


then write flow of execution for both the programs.

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

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)

Answer =

(i)
Output:-

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

(ii)
Output: -

[23, 35, 47, [49]]


23 35 47 [49]
True

You might also like