[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

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

  • (a) Length of the box;
  • (b) Width of the box;
  • (c) Height of the box.
  • Test it by writing a complete program to invoke it.

Short Answer: A function will be created to calculate the volume of a box with default values for its parameters. It will take the length, width, and height of the box as input parameters.

Python
def calculate_volume(length=5, width=5, height=5):
    return length * width * height

# Test Program
length = 10
width = 2
height = 3
print("Volume of the box:", calculate_volume(length, width, height))

Explanation:

  • The function calculate_volume calculates the volume of the box using the formula length × width × height. It has default values set to 5 for all parameters for testing purposes.
  • The test program demonstrates how to invoke the function with specific values for length, width, and height.
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *