CBSE Class 12 Computer Science Question 23 of 56

Functions — Question 23

Back to all questions
23
Question

Question 19

Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters:

  1. Length of box
  2. Width of box
  3. Height of box

Test it by writing a complete program to invoke it.

Solution
def calculate_volume(length = 5, width = 3, height = 2):
    return length * width * height

default_volume = calculate_volume()
print("Volume of the box with default values:", default_volume)

v = calculate_volume(10, 7, 15)
print("Volume of the box with default values:", v)

a = calculate_volume(length = 23, height = 6)
print("Volume of the box with default values:", a)

b = calculate_volume(width = 19)
print("Volume of the box with default values:", b)
Output
Volume of the box with default values: 30
Volume of the box with default values: 1050
Volume of the box with default values: 414
Volume of the box with default values: 190
Answer

def
calculate_volume
(
length
=
5
,
width
=
3
,
height
=
2
):
return
length
*
width
*
height
default_volume
=
calculate_volume
()
print
(
"Volume of the box with default values:"
,
default_volume
)
v
=
calculate_volume
(
10
,
7
,
15
)
print
(
"Volume of the box with default values:"
,
v
)
a
=
calculate_volume
(
length
=
23
,
height
=
6
)
print
(
"Volume of the box with default values:"
,
a
)
b
=
calculate_volume
(
width
=
19
)
print
(
"Volume of the box with default values:"
,
b
)
Output
Volume of the box with default values: 30
Volume of the box with default values: 1050
Volume of the box with default values: 414
Volume of the box with default values: 190