CSE 110: Introduction to Programming

Learning Activity (2 of 2): Function Practice

Overview

Earlier in the course, you completed a team activity to compute the areas of squares, rectangles, and circles. Please refer to the details of that activity: Team Activity: Areas of Shapes.

For this activity, you are going to repeat the earlier calculations, but this time, you will make three functions, one to calculate each of the areas.

Activity Instructions

Write functions to compute and return the areas of squares, rectangles, and circles. These functions should not display the values directly, but rather should return them, so they could be used in other parts of the program.

  1. Write a function compute_area_square that accepts a single value as a parameter, and then computes the area and returns it.

    Below the function, write code to prompt the user for the side of a square and save it into a variable, then pass this variable to the function to compute the area. Finally, get the result back from the function and display it.

  2. Repeat the previous step to write and test the functions compute_area_rectangle and compute_area_circle.

  3. Write a loop to ask the user what kind of shape they have, then prompt for the length of a side, or sides, or radius, and then calls the appropriate function, and displays the result. The program should continue looping until the user enters "quit" for the shape.

  4. Recognize that you can compute the area of a square by passing the task along to a function that computes the areas of rectangles, by giving it the side of the square twice.

    Change your program so that the compute_area_square function doesn't compute the area directly, but instead calls the compute_area_rectangle to do the work. It should pass the square side length to it (twice) and then return the value that the compute_area_rectangle function computes.

Remember, you should NOT have any print statements in the body of these functions. They should return the value, not print it.

Sample Solution

When your program is finished, please view the sample solution for this program to compare it to your approach.

You should work to complete this checkpoint program first, without looking at the sample solution. However, if you have worked on it for at least an hour and are still having problems, you may feel free to use the sample solution to help you finish your program.

Submission

You have now completed all of the learning activities for the week!

Make sure to:

Up Next

Other Links: