Lingnan University · FACULTY OF COMPUTER SCIENCE

CDT501 Chap.5 Functions, Decisions and Boundary Tests

- one subject, every graph, every model, every mark
5 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 8 · CDT501

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.

In this chapter

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

Q [7 marks]. A pricing function puts age thirteen in the child category although the standard category begins at thirteen. Repair and test it. The mark allocation used here guides AskSia practice and is not an official University scheme.
The left path ends the chain.The left path ends the chain. Once condition A is true, its block runs and later elif conditions are skipped; the right path continues only after a false result.An if-elif chain stops at the first true conditioncondition A?truefalserun A, then stoptest condition Btwo separate if statements would test both branches instead
The left path ends the chain. Once condition A is true, its block runs and later elif conditions are skipped; the right path continues only after a false result.
  • 2Write the intervals and endpoint ownership.
  • 2Change the first comparison.
  • 3Test both sides of both thresholds.
Child is below thirteen, so the first condition uses age less than thirteen. The next branch applies below sixty-five after the first has failed, covering thirteen through sixty-four. Verify twelve, thirteen, sixty-four and sixty-five to expose either inclusive-boundary mistake.
Sia tip — Write intervals before comparison symbols.
Glossary

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

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 9 of your Lingnan University subjects - and 1,000+ Bibles across every Australian university.
Sia - your CDT501 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
CDT501 · Computer Systems and Programming Basics - independent study guide on the AskSia Library. More Lingnan University subjects · Microeconomics across all universities