Introduction to Computer Science/4.7 Formative Activity - "The Area Calculator" Function

4.7 Formative Activity - "The Area Calculator" Function

🎯 Activity Goals
  • Write a complete function from scratch that accepts parameters and returns a value.
  • Write a full, professional docstring for the function you create.
  • Share your work and learn from your classmates' solutions.

Practice Task: The "Area Calculator" Function

It's time to combine all the skills we've learned in this unit. For this activity, you will create a single, well-documented, and reusable function that performs a simple calculation.

Your Task

Write a complete Python function named calculate_area that meets the following requirements:

  1. It must accept two parameters: length and width.
  2. It must calculate the area of a rectangle by multiplying the length and width.
  3. It must return the calculated area. (It should NOT print anything inside the function).
  4. It must include a complete, multi-line docstring that describes the function's purpose, its parameters, and what it returns.

You should also include a few lines of code after your function to call it with some test values and print the results, just to prove that it works.

Example of how to call it (for testing):

# This code goes OUTSIDE and AFTER your function definition
area1 = calculate_area(10, 5)
area2 = calculate_area(20, 3)

print(f"The area of a 10x5 rectangle is {area1}")
print(f"The area of a 20x3 rectangle is {area2}")


How to Submit (Code Showcase)

This is a formative activity to share with your peers.

  1. Step 1: Post Your Code. Navigate to the "Unit 4 Code Showcase" Discussion Forum. Create a new thread and paste your complete, working Python code (both the function definition and your test calls) into the body of the post.
  2. Step 2: Review and Comment. Read through the posts from at least two of your classmates. Leave a positive, constructive reply on their threads.
Plan Your Docstring!

The docstring is the key skill we are practicing today. A good docstring for this function would look like this. Use it as a model for your own!

"""Calculates the area of a rectangle.

Args: length (int or float): The length of the rectangle. width (int or float): The width of the rectangle.

Returns: int or float: The calculated area of the rectangle. """

Interactive Sandbox

Use this space to practice writing code and complete the "Your Turn" activities above.

Python 3Initializing...
Loading code editor...
Program output will appear here...