SECTION 10 / 17

SearchBirthday: inputs, return values and side effects

2 min read
On this page 1 headings
SearchBirthday needs a target birthday and eligible other students. Its contract also states what card order and markers must survive.
SearchBirthday needs a target birthday and eligible other students. Its contract also states what card order and markers must survive.

A procedure lets the caller delegate a well-defined task. SearchBirthday(Target, OtherCards) returns true if at least one card in OtherCards has the target day and month; otherwise it returns false. For an individual student search, OtherCards must exclude that student. The caller can pass only later unprocessed cards to avoid repeated pairs.

REAL COURSE DATA · THINK BEFORE EACH STEP

SearchBirthday with a clear contract

Trace the records, compare the methods and watch what changes. Back and Reset let you try again.

Ready

CURRENT RECORD X

Ready to begin

Choose Next step to follow the actual course records.

What we remember

Processed
0

Start with Next step. Predict what changes before reading the explanation.

Procedure SearchBirthday(Target, OtherCards):
    for each card Y in OtherCards:
        if BirthdayKey(Y) == Target:
            restore any required order and markers
            return true
    restore any required order and markers
    return false
End Procedure

Returning false inside the loop after the first non-match is a bug: a later card may match. Returning true after a genuine match is valid for existence. Early return must still honour the restoration part of the contract.

Three possible contracts

ContractWhat the helper may change
Answer only; free reorderingIt can reorder cards if the caller does not rely on order
Preserve markers / processed statusIt can reorder where allowed, but must keep each card’s status
No observable side effectsRestore original values, markers and exact order

In the lecture, sideways cards mark students already processed. Losing that orientation makes the caller forget which outer cards are finished. A correct Boolean alone is not enough. The caller may rely on guarantees written in the contract, but must not assume stronger guarantees that were never promised.

The target birthday is a parameter. A marked deck can also carry a parameter implicitly: the mark tells the helper which position or card matters. Written code is usually clearer when it names the target, eligible records and preservation rules explicitly.

Try it yourself

Why is SearchBirthday called with the selected student still in OtherCards wrong?

Show explained answer

The student always matches their own birthday, so every search would report true even when no second student matches.

Try it yourself

Can a helper return the correct answer and still violate its contract?

Show explained answer

Yes. It may lose processed markers or reorder words when preservation was promised. The caller’s later work can then fail.

Try it yourself

A helper checked three non-matches out of 29 cards. May it return false?

Show explained answer

No. A false result requires exhausting the eligible cards. A true result needs only one genuine match.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑