Preeti Arora Chapter 2 Functions unsolved questions Computer Science Class 12 Comprehensive guide

Chapter 2 Functions unsolved questions Computer Science Class 12

In this article, I am going to discuss Comprehensive solutions Preeti Arora Chapter 2 Functions unsolved questions computer science class 12. So let us begin!

Chapter 2 Functions Preeti Arora Class 12 Unsolved Questions Computer science || Solution Chapter 2 Functions unsolved questions Computer Science Class 12

[1] A program having multiple functions is considered better designed than a program without any function. Why?

[2] Write a function called calculate_area() that takes base and height as input arguments and returns the area of a triangle as an output. The formula used is:

Triangle Area = 1/2*base*height

Ans.:

``````def calculate_area(base,height):
return 1/2*base*height

b=int(input("Enter base:'))
h=int(input("Enter Height"))
area=calculate_area(b,h)
print("The area of triangle is:",area) ``````

[3] Modify the above function to take a third parameter called shape type. The shape type should be either triangle or rectangle. Based on the shape, it should calculate the area.
Formula used: Rectangle Area = length*width

Ans.:

``````def calculate_area(p1,p2,shape):
if shape=='triangle':
return 1/2*p1*p2
elif shape=='rectangle':
return length*width
else:
priint("Invalid Shape, Shape must triangle or rectangle")

s=input("Enter shape:")
a=int(input("Enter parameter1:"))
b=int(input("Enter parameter2:"))
area=calculate_area(a,b,s)
print("The area of ",s, " is:", area)
``````

[4] Write a function called print_pattern that takes an integer number as an argument and prints the following pattern.

If the input number is 3.
*
* *
* * *

If input is 4, then it should print:
*
* *
* * *
* * * *

Ans.:

``````def print_pattern(n):
for i in range(1,n+1):
for j in range(1,i+1):
print('*',end='')
print("\n")

x=int(input("Enter no. of lines:"))
print_pattern(x)
``````

[5] What is the utility of: –
(I) default arguments (II) Keyword arguments

[6] Describe the different types of functions in Python using appropriate examples.