Creating functions (Part 2)

Example: painting rooms

As an example, consider again the problem of painting the walls of 20 identical rooms. The calculation in the painting example is given below. Our aim is to find the area of the walls of the 20 rooms, each with dimensions 6 × 8 and 10-foot ceilings.

The area of all rooms = 20 × The area of one room

The area of one room = 2 × ( The area of big wall + The area of small wall )

The area of the big wall = 8 × 10

The area of the small wall = 6 × 10

To make this more useful, let's write a function that makes the calculation for any number of such rooms. Since the number of rooms is what changes, the number of room will be our parameter.

Creating a function

For our function header, we need to choose names for the function and all parameters. Let's call the function paint and the parameter rooms.

Our function header is define paint(rooms)

Inside the function body, we'll define some variables. These correspond to the subtasks we identified earlier.

So our variables are given by one ← 2 * (big + small) big ← 8 * 10 small ← 6 * 10

We've reordered the variable assignments so that when we define "one", "big" and "small" have already been defined and use the word "return" to identify the output: define paint(rooms) big ← 8 * 10 small ← 6 * 10 one ← 2 * (big + small) return rooms * one


Using a constant

Let's modify our function by defining the height of the walls as a constant. The constant HEIGHT is defined before the function is defined. We keep the same function header, but now we use HEIGHT instead of 10 in the assignments of values to big and small. The rest of the function body is unchanged. HEIGHT ← 10 define paint(rooms) big ← 8 * HEIGHT small ← 6 * HEIGHT one ← 2 * (big + small) return rooms * one


Recipe for defining functions

We will call functions that we create user-defined functions, as distinct from built-in functions. Here are the steps required to define one. First, we choose function and parameter names, ideally good ones. To make sure we receive the right types of inputs and produce the type of output we want, we should specify all types. We write the function header first, and then figure out the code for the function body.

Function definition recipe

  • Choose function and parameter names.
  • Choose the types of the parameters and the type of the output (if any).
  • Write the function header, which specifies the names.
  • Write the function body, the code for the function.

Number of cans

Let's use our recipe on the example of determining the number of cans to paint the walls, again using the number of rooms as a parameter. Since we've already defined a function that computes the total area, we'll use that in our function definition. The problem is outlined below.

How many 5-gallon paint cans do we need to paint 20 rooms, each with dimensions 6 × 8 and 10-foot celilings? Assume that a 1-gallon can cover an area of 350 square foot.

Number of cans needed = Total area of the walls ÷ Area covered by one can

Total area of the walls = All rooms (previous problem)

One can coverage = 5 × 350

Use the paint function to compute total area.


Following the recipe

Function definition recipe

  • Choose function and parameter names.
  • Choose the types of the parameters and the type of the output (if any).
  • Write the function header, which specifies the names.
  • Write the function body, the code for the function.

For our first step, we choose the name cans for the function and places for the number of rooms. We expect the places to be an integer and also our output, the number of cans.

Function: cans

Parameter: places

Input type: integer

Output type: integer


In our function header, the name cans is followed by the name of the parameter, in parentheses, and in the function body, we call the function paint on places: define cans(places) area ← paint(places) coverage ← 5 * 350} return ceiling(area / coverage)

Notice that we use the ceiling function to round up to an integer for our output.