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


C(n)=250n,n∈{0,1,2,…}.C(n)=250n,\qquad n\in\{0,1,2,\ldots\}.


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.


Python
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


Six connected boxes show the progression from a signature to tests.


Use tests as evidence

Turn your examples into executable checks. In Python, an assertion can compare a returned value with its expected value:


Python
assert material_cost(0) == 0
assert material_cost(3) == 750
assert material_cost(5) == 1250


When 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:


Python
# cut_fee: int -> int


Its 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:


400+75(8−6)=400+150=550 cents.400+75(8-6)=400+150=550\ \text{cents}.


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:


Python
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:


Python
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.


fee=400 cents+(75 cents/min)(2 min)=550 cents.\text{fee}=400\ \text{cents}+ (75\ \text{cents/min})(2\ \text{min}) =550\ \text{cents}.


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=475400+75=475 cents. Eight minutes add two charged minutes, giving 550 cents.


Python
assert cut_fee(0) == 400
assert cut_fee(6) == 400
assert cut_fee(7) == 475
assert cut_fee(8) == 550


Checking 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 school needs a function that returns the number of five-seat vehicles required for a nonnegative integer passenger count. Arrange the following activities in the Systematic Function Design sequence used in this course, which records the input and result types before stating the purpose, and complete the planned examples before writing any code, including the template.

  1. 1.

    Specify that the input passenger count and returned vehicle count are integers.

  2. 2.

    Run checks comparing the returned vehicle counts with the expected results.

  3. 3.

    Replace the unfinished expression with the calculation for the vehicle count.

  4. 4.

    Calculate expected results for representative passenger counts from the requirement.

  5. 5.

    Write a function template with the input parameter and an unfinished return expression.

  6. 6.

    State the function's purpose, including valid inputs and what the result counts.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Each artifact below is produced while designing a function systematically. Match each artifact to the description of the job it does in the design.

Match each term
A.

Lists the types a function consumes and the type it produces, written like Number Number -> Boolean

B.

A skeleton body, derived from the input type, showing which pieces of the input data the code can use

C.

Concrete calls paired with expected results that are worked out by hand before the body is coded

D.

States how information from the problem is represented as program values, such as a temperature stored as a number of degrees Celsius

E.

One or two sentences describing what result the function produces from its inputs, in terms of the problem

F.

A header whose body returns a placeholder value of the right type, so the file runs before the real code exists

Stub

Signature

Data definition

Examples

Purpose statement

Template

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice