CDT501 Chap.5 Functions, Decisions and Boundary Tests
Functions, Decisions and Boundary Tests
Turn verbal rules into testable contracts
This chapter separates returned values from printed output, maps positional arguments to parameters and translates verbal intervals into ordered if-elif-else conditions. It tests the values immediately beside every threshold, then uses Boolean grouping and short-circuit evaluation without hiding the underlying rule.
Leap-year exceptions show why ordinary examples do not prove boundary correctness.
What this chapter covers
- 01
Function inputs and return contracts
- 02
Print versus return
- 03
Comparisons and Boolean logic
- 04
If, elif and else ordering
- 05
Boundary and exception tests
Worked example · free
Repair an inclusive pricing boundary
- 2Write the intervals and endpoint ownership.
- 2Change the first comparison.
- 3Test both sides of both thresholds.
Key terms
- Parameter
- A name in a function definition that receives an argument during a call.
- Argument
- A value supplied to a function parameter when the function is called.
- Boolean
- A truth value, either True or False, produced by comparisons and logical operations.
- Short Circuit
- Stopping Boolean evaluation once the final truth value is already determined.
- Override Path
- A more specific decision branch that must be checked before a broader remaining category.
Functions, Decisions and Boundary Tests FAQ
What is the difference between print and return?
Print displays a representation for a person and usually returns None. Return sends a value back to the caller so it can be stored, combined or tested. Keep reusable calculation inside the function and let the caller decide whether and how the returned result should be displayed.
Why can two if statements produce two actions?
Each independent if statement is checked even when an earlier one was true. An if-elif chain stops after its first true condition, which suits mutually exclusive categories. Choose the structure from the rule: separate actions may both apply, while one category among alternatives should stop further selection.
Which inputs are most useful for a threshold rule?
Use a value immediately below, exactly at and immediately above each point where behaviour changes. Add the smallest valid input and one invalid value if the domain has a boundary. These cases distinguish inclusive from exclusive comparisons, while an ordinary middle input often passes both correct and incorrect implementations.
Why test an invalid value before the category branches?
A category chain should classify only values that belong to its stated domain. If an invalid value falls through to a final else branch, the program may confidently label it as the last valid category, hiding the original data problem. Check the domain boundary first and return or raise a clear error; then let the remaining branches express mutually exclusive valid cases.
This order separates validation from classification and makes the final else meaningful. Tests should include the lowest and highest valid values plus at least one value just outside each boundary. The same principle applies to types and missing inputs: reject or handle them explicitly before numerical comparisons produce a misleading category or an accidental exception.
Assessment move
For each function, write a one-sentence contract with input, output and invalid-input behaviour. Draw every category as an interval and label endpoint ownership before writing conditions. Trace each return path and verify a consistent returned type. Build a test table naming the rule exercised by every input, including exception cases such as century leap years. Start with a function that currently prints its calculation.
Assign the call to a variable and inspect both the screen output and stored value, then replace print with return and move presentation into the caller. This exercise makes the interface difference observable. For positional arguments, use two values of the same type whose reversal changes the result, and then call the function with keywords to make the binding explicit.
Convert each verbal category rule into non-overlapping intervals. Write the lower and upper endpoints, decide which comparisons include equality and place invalid-input rejection before the ordinary categories. Use an if-elif chain when exactly one category should apply, and two independent if statements only when both actions may be required. For compound conditions, write the grouping in words before using and, or and not.
Give a long expression a Boolean name that describes the decision rather than the syntax. Your test table should contain the value below, at and above every threshold, one interior value per interval and one invalid value. Add a column explaining the distinction each case exposes. For leap years, include ordinary divisible and non-divisible years plus century exceptions so every logical path is exercised.
Finish by checking that all successful paths return the same promised type. A branch that returns a message while others return numbers should be redesigned or rejected explicitly before later arithmetic receives it.
Working through Functions, Decisions and Boundary Tests in CDT501? Sia is AskSia’s AI Computer Science tutor — ask any CDT501 Functions, Decisions and Boundary Tests question and get a clear, step-by-step explanation grounded in how CDT501 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.