Quarry School

Compose two small functions — quick review

In plain words

A stall at a market works out a bill in three small jobs.

Rule

Python works out every argument of a call before the call begins. When an argument is itself a call, that call finishes first and its return value becomes the argument of the call that receives it. For the receiving call to get a value other than None, the inner function has to return one.

Step

  1. Write the task as a list of small jobs, each one taking some values and giving one value back.

  2. Write one function for each job, with a parameter for each value it takes. Return its result.

  3. Test each function that calls no other function alone, with a few arguments, including any boundary value. Compare with results worked out by hand.

  4. Join the functions by passing one call as the argument of another, or by giving a result a name and passing the name. Then test the joined function in the same way.

  5. Trace a joined line by working out each argument before the call that receives it, and replace each finished call with the value it returns.

Trap

A function that prints where it should return breaks the chain. If subtotal displayed the product and had no return line, with_fee would receive None. The comparison None < 20 would then stop the program with a TypeError, because Python provides no order comparison between None and a number. Return from every function whose value another function needs.

The other slip is one long function that does every job. Split it where one value is finished and the next job begins, so that each job can be called and tested by its own name.