What's a helper function?

A helper function is a function that performs part of the computation of another function. Helper functions are used to make your programs easier to read by giving descriptive names to computations. They also let you reuse computations, just as with functions in general.

As a first example, consider another version of the wages program that pays a person different amounts for overtime depending on the number of overtime hours that they worked:

; wages2 : number -> number
; consumes number of overtime hours worked and produces pay
(define (wages2 overtime)
  (cond [(< overtime 5) (+ (* 40 10) (* overtime 15))]
        [(< overtime 10) (+ (* 40 10) (* overtime 20))]	
        [else (+ (* 40 10) (* overtime 25))]))

Note that this example repeats the same computation several times in the cond. We could make this much cleaner by introducing a function for the repeated computation in the cond answers. Let's call that function pay

; pay : number number -> number
; consumes overtime hours and rate and produces pay
(define (pay overtime overtime-rate)
  (+ (* 40 10) (* overtime overtime-rate)))

This function needs two inputs: overtime-rate is the data that changes each time we use the expression. Overtime is also an input because inputs are how we pass data from one function to another.

We can now rewrite wages2 using pay:

; wages2 : number -> number
; consumes number of overtime hours worked and produces pay
(define (wages2 overtime)
  (cond [(< overtime 5) (pay overtime 15)]
        [(< overtime 10) (pay overtime 20)]	
        [else (pay overtime 25)]))

The function pay is a helper function because it helps us write wages2 cleanly.


As another example, consider the pens example from lecture 4. Here is one version of the printed-pen-cost code, written to account for an empty slogan.

;; printed-pen-cost : number string -> number
;; consumes number of pens to order and a slogan and produces total order cost
(define (printed-pen-cost num-pens slogan)
  (cond [(string=? slogan "") (* num-pens (+ (* .02 (string-length slogan))
                                             (single-pen-cost num-pens)))]
        [else (+ (* num-pens (+ (* .02 (string-length slogan))
                                (single-pen-cost num-pens)))
                 3)]))

This is pretty ugly code though! Notice that there is a large chunk of code repeated in the two cond answers. You should create a helper function for the repeated code. Here's an example of this code written cleanly with a helper function (pens-cost).

;; pens-cost : number string -> number
;; produces cost of the given number of pens
(define (pens-cost num-pens slogan)
  (* num-pens (+ (* .02 (string-length slogan))
                 (single-pen-cost num-pens))))

;; printed-pen-cost : number string -> number
;; consumes number of pens to order and a slogan and produces total order cost
(define (printed-pen-cost num-pens slogan)
  (cond [(string=? slogan "") (pens-cost num-pens slogan)]
        [else (+ (pens-cost num-pens slogan) 3)]))

This new version is also easier to change if the formula for pen pricing changes because the formula is written in only one place instead of two.

After homework 1, we expect you to use helper functions to reuse repeated computations and to improve the readability of your code.

Summary

Whenever you see the same computation multiple times in a program, you should create a separate (helper) function for that computation and call that function instead. This


1101 Home Page