Composing Functions
A number of students have had trouble with the "composing functions"
section of the textbook (the section with the movie-theatre example).
Here are some additional, simpler examples to practice on. They are
not homework to be turned in (indeed, I've provided
my solutions for you to look at after you've written your own),
but examples for you to try if you're having trouble with the section.
- According to one survey, teenage boys spend an average of 32% of
their allowance on food, while teenage girls spend 26% on average. If a
boy and a girl each receive n dollars, how much more does the
boy spend on food? Write three functions: the first
consumes an amount of allowance and returns how much a boy spends on
food. The second consumes an amount of allowance and returns how much a
girl spends on food. The third consumes an allowance amount and returns
the difference between how much a boy and a girl spend on food.
My solution.
- Write a function to calculate a person's overtime
pay, given the person's pay rate, the number of overtime hours, and the
pay differential. For example, if
a person works 4 hours overtime, their regular rate is $10 and the pay
differential is 1.5 (time-and-a-half), then their overtime pay is $60.
Then calculate the person's gross pay, given their pay rate, the number
of regular hours, the number of overtime hours worked and the pay
differential: for this same example, if the person also worked 40
regular hours, the gross pay would be $400 + $60 = $460.
My solution
More examples, and my solutions, when I get around to typing them in....
Last modified:
Thu Sep 16 16:25:01 EDT 1999
Stephen Bloch / sbloch@adelphi.edu