The Design Recipe
Design a Function from Its Requirements
Systematic Function Design gives you a repeatable way to turn a requirement into a small, testable function. You describe the inputs and result, predict examples, build a template, write the body and check its behaviour. Each stage answers a different question, so you can locate a mistake instead of repeatedly changing code without knowing what should happen.
Describe the function
A signature states the types of inputs a function accepts and the type of result it produces. For example, material_cost: int -> int describes one integer input and an integer result. Types alone do not tell you what the integers represent, so you also need a purpose statement.
The purpose should identify what the function returns, what its inputs mean and any restrictions on valid inputs. Include units when they matter. A nonnegative item count is more precise than simply an integer. These restrictions describe the function's domain.
Example: A workshop buys booklets at 250 cents each. The function material_cost(n) returns the total cost in cents for a nonnegative integer number of booklets, with no other fees. Its governing relationship is
Whether the function returns a value or prints a message matters. A returned value can be used in another calculation; printing only displays information.
Predict results before coding
Work out examples directly from the requirement. For the booklet function, zero booklets cost 0 cents, three cost 750 cents and five cost 1250 cents. These predictions give you an independent reference when you later run the program.
Include ordinary inputs and relevant special cases. Zero is useful when it is valid. If a requirement changes at a threshold, include the threshold and inputs immediately on either side. A negative count is outside this function's stated domain, so its behaviour need not be invented unless handling invalid inputs is part of the requirement.
Build from a template
A template is a partial function structure that identifies the available inputs and leaves the calculation unfinished. It helps you separate the function's shape from the details of its body.
def material_cost(n: int) -> int:
# Use n to construct the result.
return ...Here, ... is a placeholder, not the completed calculation. Replacing it with 250 * n produces the intended body. For a function with different cases, the template may also identify where decisions are needed.
Keep the artifacts consistent:
Artifact | What to check |
|---|---|
Signature | Input and result types |
Purpose | Meaning, units and valid inputs |
Examples | Predicted input-output pairs |
Template | Available inputs and required structure |
Tests | Actual results compared with predictions |
Use tests as evidence
Turn your examples into executable checks. In Python, an assertion can compare a returned value with its expected value:
assert material_cost(0) == 0
assert material_cost(3) == 750
assert material_cost(5) == 1250When a check fails, inspect the requirement, expected result and function body. A hand calculation can be wrong too. Correct the artifact that conflicts with the requirement, then rerun the checks.
Passing these checks shows agreement for the tested inputs. It does not prove the function works for every valid input. Use the calculation or case structure to reason about the remaining inputs as well.
What to remember
- State types, meaning, units and input restrictions.
- Predict examples before implementing the body.
- Use a template to organize the calculation.
- Compare actual results with independent expectations.
- Treat passing tests as evidence with a limited scope.
Example: A Makerspace Cutting Fee
A makerspace charges a booking fee of 400 cents for a laser-cutting session lasting up to and including six minutes. Each whole minute beyond six adds 75 cents. A zero-minute booking still incurs the booking fee. Sessions are recorded as nonnegative integer numbers of minutes, with no other charges.
Use Systematic Function Design to create cut_fee(minutes), which returns the fee in cents. An expected result is an output calculated from the requirement before running the implementation.
a) State the signature and purpose, then predict results for sessions of 0, 6 and 8 minutes.
The function accepts an integer number of minutes and returns an integer number of cents:
# cut_fee: int -> intIts purpose is to return the cutting-session fee in cents for a nonnegative integer duration, including the 400-cent booking fee and 75 cents for each minute beyond six.
For zero minutes, the booking fee still applies, so the expected result is 400 cents. Six minutes are included in the booking fee, so that expected result is also 400 cents.
For eight minutes, two minutes are charged additionally:
The predicted input-output pairs are (0, 400), (6, 400) and (8, 550).
b) Write a template, complete the function body and trace the eight-minute example.
The input duration determines whether an additional charge applies. Begin with a template that separates the two cases:
def cut_fee(minutes: int) -> int:
if minutes <= 6:
return ...
return ...The first case returns the booking fee. In the second case, subtract the included minutes before multiplying by the additional rate:
def cut_fee(minutes: int) -> int:
if minutes <= 6:
return 400
return 400 + 75 * (minutes - 6)For an eight-minute session, the extra duration is ________, found by subtracting six minutes from eight minutes. The additional charge is ________, found by multiplying that duration by 75 cents per minute.
The function returns 550 cents for an eight-minute session, agreeing with the prediction.
c) Write executable checks for 0, 6, 7 and 8 minutes. Explain why checking only zero minutes would be insufficient.
Calculate each expectation from the fee rule. Zero and six minutes each cost 400 cents. Seven minutes add one charged minute, giving 400+75=475 cents. Eight minutes add two charged minutes, giving 550 cents.
assert cut_fee(0) == 400
assert cut_fee(6) == 400
assert cut_fee(7) == 475
assert cut_fee(8) == 550Checking only zero minutes exercises the booking-fee case. It does not check the included-time limit or the additional-minute calculation. The six-minute check covers the limit, while seven minutes checks the first duration that incurs an additional charge.
These checks all pass for the completed function. Their expectations remain useful because they come from the fee requirement, rather than being copied from the program's output.
A theatre requires available_seats(capacity, booked) to return an integer count of seats still available, without printing anything. Both inputs are nonnegative integers, and booked does not exceed capacity. Which purpose statement, describing what the function does, best matches this requirement?