Working with Functions in Python Assignment Solutions

Assignment solutions for Working with Functions in Python Chapter of CBSE Class 12. In this post we are providing, solution of un-solved assignment questions from Sumita Arora book for CBSE Class XII Computer Science (Python) students.

Short Answer Questions:
Working with Functions in Python

Q.1. A program having multiple functions is considered better designed than a program without any functions. why?

Answer:
A program containing multiple function has following benefits over the program without functions :
a. No need of writing code again and again .
b. Functions can called anywhere in program.
c. It can be reusable

Q.2. What all information does a function header give you about the function ?

Answer:
Function header is of this type def <Function name> ([parameters]) :
def tells that a function is being defined .
function name tells the name of function , it is also useful when the function is called.
Parameters contains the values . which can be feed into the function.

Q.3.What do you understand by flow of execusion?

Answer:
Flow of execution tells which code will run after or before the current code.

Q.4.What are arguments? What are parameters? How these two terms are different yet related? Give example

Answer:
Arguments: Passed as a value of functions for processing.
Parameters: the function contain argument variable the variable names said in the written language in python.
for example:
def sum(x,y,z):
   return (x+y+z)
sum(1,2,3)

arguments are 1,2,3.
parameter are x,y,z.

Q.5. What is the utility of :
(i)default arguments
(ii)keyword arguments?

Answer:
(i) Default argument are those arguments which are passed by default as argument to the function.
(ii)The named arguments which have assigned values being passed in the function call statements.

Q.6. Explain with a code example the usage of default argument and keyword arguments.
Answer:
def sum(x,n,y=5,z=0)
    return(x+y+z+n)

Here y and z has default value which are called a s default argument during calling of the function.
sum(9,8)

sum(n=1,y=5,x=4)
Here values given to the functions are unordered. This type of parameter assignment through keyword is known as keyword arguments.

Q7. Describe the different styles of functions in Python using appropriate examples.

Answer:
1.Built-in function – these function are already present in the python idle and no special file is needed to open the function.
2.Module function – These modules are called- they are already stored or called from file from internet or from any storage containing the module. They containing many of the functions required in programming.
3.User Defined function – These functions are defined by the user in the program/project he is writing.

Q8. Differentiate between fruitful functions and non-fruitful functions.

Answer: 
Fruitful functions - These functions return values.
 sum(x,n,y=5,z=0)
    return(x+y+z+n) 
non-fruitful functions- These function does not return a value.
 sum(x,n,y=5,z=0)
    x+y+z+n  #nothing is return
Q.9.Can a function return multiple values how?
Answer:
summultiply(x=1,n=2,y=5,z=0)
   return(x+y+z+n, x*y*n) 
summultiply() 
Both of the variable are calculated returned by the function.

Q.10.What is scope? What is the scope resolving rule of Python?

Answer:
Suppose  
p=10
summultiply(x=1,n=2,y=5,z=0)
     p=1
     return(x+y+z+n, x*y*n) 
summuliply()
print(p)
   Here, the above p is global variable function which will also be in output as it is defined in the area the where there is no indentation.This area is called its scope.p=1 is in local scope of the program . The scope resolving rule of Python is LEGB rule.

Q.11.What is the difference between local and global variables?

Answer:
 Suppose  
p=10
summultiply(x=1,n=2,y=5,z=0)
     p=1
     return(x+y+z+n, x*y*n) 
summuliply()
print(p)
   Here, the above p is global variable function which will also be in output as it is defined in the area the where there is no indentation. This area is called its scope .p=1 is in local scope of the program. hence it is local variable.
Global variable is used throughout the program.
local variable is accessible only in its scope defined by indentation.

Q.12. When is global statement used? Why is its use not recommended?

Answer:
Global variable scope is of whole program . What is initialized first cannot be changed in the local scope of program only in global scope variable can change(where there is no indentation).This is why its use not recommended.

Q.13.Write the term suitable for following description:

(a) A name inside the parentheses of a function header that can receive a value.
Answer:
Parameter.

(b) An argument passed to a specific parameter using the parameter name.
Answer:
 Keyword argument

(c) A value passed to a function parameter
Answer:
Argument.

(d) A value assigned to a parameter name in the function header.
Answer:
 Default argument.

(e)A value assigned to a parameter name in the function call.
Answer:
Keyword argument.

(f) A name defined outside all function definitions.
Answer:
Global variable.

(g) A variable created inside a function body
Answer:
 Local variable.

Type B : Application Based Questions : Working with Functions in Python

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)
Answer (a)-
total=0
def sum(arg1,arg2):   #def should not be intended it's content are intended.
    total=arg1+arg2          # ; is not used for end of line in python
    print("Total:",total)
    return total              # ; is not used for end of line in python , return should be intended as it gives value 
sum(10,20)               # ; is not used for end of line in python 
print("Total:",total) 

output-
Total:30
Total:0
(b)
def Tot(Number)
    sum=0
    for C in Range (1,Number+1):
        sum+=c
    RETURN sum
print(Tot[3])
print(Tot[6]) 

Answer(b)-
corrected code-

output-
6
21

Q.2.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->7->10->

Q.3. What will the following function return?
def addEm(x,y,z):
print(x+y+z)

Answer:
It is a void function thus it returns nothing.

Q.4. What will the following function print when called?
def addEm(x,y,z):
return x+y+z
print(x+y+z)

Answer:
It will print nothing. As the function return value is in second line and there no initialization.

Q.5. What will be the output of following programs?
i)
num=1
def myfunc():
    return num
print(num)
print(myfunc())
print(num)
Output:
1
1
1
ii)
num=1
def myfunc():
    num=10
    return num
print(num)
print( myfunc())
print(num)
Answer:
output-
1
10
1
(iii)
num=1
def myfunc():
    global num
    num=10
    return num
print(num)
print( myfunc())
print(num)
Answer:
output-
1
10
10 
(iv)
def display():
    print("Hello",end=' ')
display()
print("there!")
Answer:
output-
Hello there!
Q6.
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
Q.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:
Here return is before print statement . so when we will call  function it will return nothing as the print statement is after return.
Q.8. Write a function namely fun that takes no parameters and always returns None.
Answer:
def fun():
    return None
print(fun())

Q.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?
Answer:

5 times 5 = 25
(ii) What is variable output equal to after the code is executed?
Answer:

25

Q.10. Consider the code below and answer the questions that follow:
def multiply(number1,number2):
answer=number1*number2

return(answer)
print(number1,’times’,number2,’=’,answer)
output=multiply(5,5)

(i) When the code above is executed, what prints out?
Nothing is printed because return statement is written before print statement.
(ii) What is variable output equal to after the code is executed?
Answer:

25

Q.11. Find the errors in code given below:
(a) def minus(total,decrement)
output=total-decrement
print(output)
return(output)

Answer:
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
Answer:
def 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") :) #calling of function in wrong way
print(beta (string = 'true' ))
print(alpha(n = 5, "Good-bye") :)

Answer:
print(alpha(“Valentine’s Day”) 🙂 #calling of function in wrong way

Q.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:
Working with Functions Solutions Class 12

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

Q.14.In the following code which variables are in same scope?
def func1():
a=1
b=2
def func2():
c=3
d=4
e=5

Answer:
a,b are in same local scope.
c,d are in same local scope.

Q.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:
i=0 
def fun(x):
    print (x / 2)

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

Q.16.Write a program with non-void version of above function and then write flow of execution for both the programs.

Answer:
i=0
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
#print(i)


Flow of execution:

1 --> 3 --> 1 --> 2 --> 3 --> 4 --> 1 --> 2 --> 4 --> 5 --> 1 --> 2 --> 5 --> 6 --> 1 --> 2 --> 6 --> 7 --> 1 --> 2 --> 7

Q.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)
Answer:
Output:-
[1, 2, 3, [4]] [1, 2, 3, [4]]
(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:
Output: –
[23, 35, 47, [49]]
23 35 47 [49]
True

Programming Practice/Knowledge based Questions :

Q.1.Write a function that takes amount-in-dollars and dollar-to-rupee conversion price ; it then returns the amount converted to rupees. Create the function in both void and non-void forms.

void form-

Non-void form-

Q.2.Write a function to calculate volume of a box with appropriate default values for its parameters .Your function should have the following input parameters:(a) length of box ; (b) width of box ; (c) height of box.

Answer:
def vol( l = 1, w = 1 , h = 1 ) :
    return l * w * h

intx=0

length = int(input("Enter the length : "))
width = int(input("Enter the width : "))
height = int(input("Enter the height : "))

#print(intx)

print("volume of box = ", vol( length , width , height ))

Q.3. Write a program to have following functions :

(i) a function that takes a number as argument and calculates cube for it .The function does not return a value .If there is no value passed to the function in function call , the function should calculate cube of 2.

(ii) A function that takes two char arguments and returns True if both the arguments are equal otherwise False . 

 Test both these functions by giving appropriate function call statements
Answer:

#(i) 

def cube( a = 2 ) :
    print( "Cube of ", a ,"="  , a ** 3 )

num = input("Enter a number (For empty press Enter ) :")

intx=0
if num == "" :
    cube()
else :
    cube( int (num) )

#print(intx)

#(ii)
 x=0
def chr() :
    char1 = input("Enter a Char 1 : ")
    char2 = input("Enter a Char 2 : ")
    if char1 == char2 :
        print("Ture")
    else:
        print("False ")

print(x)
chr()

Q.4.Write a function that receives two numbers and generates a random number from that range Using this function, the main program should be able to print three numbers randomly.

Answer:
import random

intx=0

def ran(a , b) :
    print( random . randint(a , b ) )
   
first = int(input("Enter  a number = "))
second = int(input("Enter second number = "))

#print(intx)
ran(first , second )
ran(first , second )
ran(first , second )

Q.5.Write a function that receives two string arguments and checks whether they are same-length strings.

Answer:
i=0
def chr(a , b) :
    print( len(a ) == len(b) )

first = input("Enter  a number = ")
second = input("Enter second number = ")
#printx into m 
chr(first , second )

Q.6. Write a function namely nthRoot () that receives two parameters x and n and returns nth root of x i.e., X**1/n . The default value of n is 2.

Answer:
def root(x , n = 2 ) :
    print( x ** (1 / n) )

intx=0
x = int(input(" Enter a number = "))
n = int(input("Enter nthRoot (For empty Enter 0 )= "))

if n == 0 :
    root( x)

#print(intx)
else :
    root(x,n)

Q.7. Write a function that takes a number n and then returns a randomly generated number having exactly n digits (not starting with zero) eg ., if n is 2 then function can randomly return a number 10-99 but 07, 02 etc. are not valid two digit numbers.

Answer:
import random

intx=0
cex=4
#print(i)
def ran( x ) :
    a = 10 ** (x - 1 )
    b= 10 ** (x )
    print( random.randint ( a , b ) )

n = int(input("Enter number of digit : "))

ran(n)

Q.8.Write a function that takes two numbers and returns the number that has minimum one’s digit.

Answer:
def min(x , y ) :
    a = x % 10
    b = y % 10
    if a < b :
        return x
    else :
        return y
intx=8
can=0
first = int(input("Enter first number = "))
second = int(input("Enter second number = "))
print ( "Minimum one's digit number = " , min( first , second ) )

Q.9. Write a program that generates a series using a function which takes first and last values of the series and then generates four terms that are equidistant e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7.

Answer:
intx=0
def ser(    a  , b ) : #function intialisarion

    d = int ( ( b - a ) / 3 )
    print("Series = " , a , a + d , a + 2*d , b )
#print(i)
first = int(input("Enter first Term = "))
last = int(input("Enter last Term = "))
ser(first , last )

Thanks for visiting and reading important questions on Working with Functions in Python Assignment Solutions Class 12 Sumita Arora Computer Science book.

Solved Assignments of Computer Science Class 12 CBSE.

Python revision tour I

CBSE MCQ TEST

Python Practical file according to CBSE syllabus

Leave a Comment

Your email address will not be published.