0
0
Cprogramming~30 mins

Reusability and maintenance - Mini Project: Build & Apply

Choose your learning style9 modes available
Reusability and maintenance
📖 Scenario: You are working on a small program that calculates the area of different shapes. To keep your code clean and easy to maintain, you want to reuse the area calculation logic by putting it inside functions.
🎯 Goal: Create reusable functions to calculate the area of a rectangle and a circle, then use these functions to print the areas for given dimensions.
📋 What You'll Learn
Create a function called rectangle_area that takes two double parameters length and width and returns their product as double.
Create a function called circle_area that takes one double parameter radius and returns the area of the circle using the formula 3.14159 * radius * radius.
In main, declare variables rect_length and rect_width with values 5.0 and 3.0 respectively.
In main, declare a variable circle_radius with value 2.0.
Call the functions rectangle_area and circle_area with the declared variables and print the results with descriptive text.
💡 Why This Matters
🌍 Real World
Reusable functions are used in real software to avoid repeating code and to make programs easier to update and fix.
💼 Career
Understanding how to write and use functions is essential for any programming job, as it improves code quality and teamwork.
Progress0 / 4 steps
1
Create variables for rectangle and circle dimensions
In the main function, create double variables called rect_length and rect_width and set them to 5.0 and 3.0 respectively. Also create a double variable called circle_radius and set it to 2.0.
C
Need a hint?

Use the syntax double variable_name = value; to create each variable.

2
Create function to calculate rectangle area
Above the main function, create a function called rectangle_area that takes two double parameters named length and width. The function should return a double which is the product of length and width.
C
Need a hint?

Define the function with double return type and multiply the two parameters.

3
Create function to calculate circle area
Above the main function, create a function called circle_area that takes one double parameter named radius. The function should return a double which is the area calculated as 3.14159 * radius * radius.
C
Need a hint?

Use the formula for circle area and return the result.

4
Call functions and print the areas
In the main function, call rectangle_area with rect_length and rect_width and store the result in a double variable called rect_area. Call circle_area with circle_radius and store the result in a double variable called circ_area. Then print both areas using printf with the exact text:
"Rectangle area: %f\n" and "Circle area: %f\n".
C
Need a hint?

Use printf("Rectangle area: %f\n", rect_area); and similarly for circle area.