Creating functions in Python (Part 2)

Example: area of record label

Determine the area of a record label with diameter 4 inches and a hole diameter 1/4 inches. Here's where I should be giving you some convincing reason why anyone would want to calculate the area of a record label, but maybe I'll leave that for you to invent. I must admit that I have written equally silly-sounding programs in the past, so it isn't that far-fetched. Let's start by sketching out what information we'd like to have. It would be useful know the area of the circle with the same radius as the label and then we could subtract off the area of the hole:

Area of record label = Area of circle with lebel radius - Area of circle with hole radius.

Developing a plan

Plan

  • Determine the radius from the diameter.
  • Determine the area of a circle.
  • Determine the radius from the diameter.
  • Determine the area of a circle.

Since we usually calculate area using radius, we'll do that first, and then calculate the area of the outer circle. Then, we'll determine the radius of the inner circle and finally its area.

Whenever we see repeated actions, we should consider using a function. Candidates for functions are calculating radius from the diameter and finding the area of a circle. But, since calculating radius from diameter is so simple, it will probably be clearer not to use a function. Is there a built-in function for the area of a circle? No, so let's write our own.

Writing the function in Python

import math

Since we're going to need to use π, we'll start by importing the math module.


LABEL_DIAM = 4            # Diameter, in inches
HOLE_DIAM = .25           # Diameter, in inches

We'll define the two diameters as constants. Since the names themselves imply that they are diameters, writing diameter into the comments isn't strictly necessary. With any other names, though, it would be crucial to add the word “diameter” in the comment to be able to remembering how to deal with the values in the calculations.


def area_circle(radius):

Since we're defining a function in our program, we define that before the rest of the program. We place all functions after the constants. We choose the function name "area_circle" and the parameter name radius with the input and output both floating point numbers.


    return math.pi * radius ** 2

After writing the function header, we write the function body. For a more complicated function, we might have also included comments with the tasks used in the function.


To structure our program, we keep our notes as comments. There should be comments for any task that isn't obvious from the identifiers and the simplicity of the calculations. Exactly what is obvious isn't so obvious.

## area of circle with label radius
outer = area_circle(LABEL_DIAM / 2)

Now we can use our function to fill in the first task, using the label diameter divided by two as the input


## area of circle with hole radius
inner = area_circle(HOLE_DIAM / 2)

We write the second task, using the hole diameter divided by two as the input.


## difference
print(outer - inner)

Finally, we determine the difference of the two areas.


Putting them all together

Here is the program when all parts are put together:

import math

LABEL_DIAM = 4            # Diameter, in inches
HOLE_DIAM = .25           # Diameter, in inches

def area_circle(radius):
    return math.pi * radius ** 2

## area of circle with label radius
outer = area_circle(LABEL_DIAM / 2)

## area of circle with hole radius
inner = area_circle(HOLE_DIAM / 2)

## difference
print(outer - inner)

Writing a program

The final order of the program should be as follows:

  • Import modules
  • Constant definitions
  • User-defined function definitions
  • Program

Import any modules that are used and then define constants, not forgetting to use capital letters and comments. After that, place all the user-defined functions used in the program, and finally, the program itself. Sometimes the program will consist only of a single call to a user-defined function.

The order of the recipe steps that we followed are not the same as the order in which the lines appear in the final program:

Program recipe

  • Import modules
  • Define constants
  • List tasks as comments
  • Create user-defined functions
    • Choose identifiers and types
    • Create function header
    • Import modules (beginning of program)
    • Define constants (beginning of function)
    • List tasks as comments
    • Fill in details of function tasks
  • Fill in details of program tasks

We do start by importing modules and defining constants, but after that, we figure out the tasks in the program, listed as comments. It is by figuring out the tasks that we realize we'd like to have some user-defined functions, which we create next. We start by figuring out the identifiers and types and creating the function header. The process of writing the function body is like writing a program, so again we figure out if we need to import any modules or define any constants, and then put in the tasks as comments. All that remains to fill in the details of the function tasks and then the program tasks, which looks need and tidy, but often isn't.

You might get part-way through defining a function when you realize that that function needs another function, or most of the way through the program before realizing you need to import another module or define another constant. All of which is perfectly fine. Using the recipe is to make sure that you don't miss any steps, not that you adhere strictly to the order given.