Learn to extend an existing program module through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Programming Concepts and Skills
ICS4U A2.3 | Add a useful capability while preserving what already works
In ICS3U, you learned to design and use program components such as functions. In Grade 12, A2.3 focuses on extending an existing program module: adding to a program component that is already in use. The key is not simply to write more code. First understand what the module is for and how other parts of a program use it. Then add a related capability, make its inputs and result clear, and check that the addition fits without removing the module’s existing behaviour. This lesson uses small Python 3 examples. They are code-reading and design examples; no code is claimed to have been run.
What you will learn
Explain what it means to extend an existing program module.
Identify the module’s current purpose and behaviour before changing it.
Add a related function that fits the module’s responsibilities.
Trace the new function and describe expected output without assuming the code has been run.
Check that existing behaviour remains available after an extension.
1. Review the existing module before changing it
A module is a program component that groups related code so it can be used as a unit. In Python, a module is commonly a file containing functions, classes, or other definitions. In this lesson, each small example represents the contents of an existing module. The same idea applies when a course uses another programming language or a different module format.
Extending a module means adding a related capability to it. For example, a module that already calculates a quiz total might be extended to calculate the average too. A new feature should belong with the module’s purpose. A feature unrelated to that purpose may be better placed elsewhere.
Before changing a module, read what is already there. Find out what its functions expect as input, what they return or display, and which existing features should continue to work. The surrounding program may rely on those details. A careful extension adds to the available behaviour rather than silently changing the meaning of an existing function.
For each addition, state preconditions (what must be true before it is used), input, the algorithm, and expected output. Then trace a specific example by hand. A trace helps you reason about the code, but it is not evidence that the code has been executed or tested.
Read the existing module and identify its purpose.
Add a capability that is related to that purpose.
Keep existing functions and their expected behaviour in view.
Describe inputs, preconditions, steps, and expected results.
2. Add a related function with a clear contract
A function’s contract is a practical description of how to use it: what input it expects and what result it provides. A good extension makes that description straightforward. Choose names that suggest the task, use parameters for information that can vary, and return a result when another part of the program needs to use it.
Keep the new function focused. For instance, a function that finds the highest score in a list should not also ask a user for input or print unrelated messages. Separating the calculation from interaction makes the function easier to understand and reuse. This is a course-level design choice, not a requirement to use any particular Python feature.
Consider both the ordinary case and the preconditions. If a function needs at least one score, say so. If an empty list is possible, decide and document how the module should handle it before adding the function. Do not let an unstated assumption become a surprise for whoever uses the module.
A useful extension also avoids unnecessary changes to existing code. If an old function already calculates a total correctly, a new average function can call it rather than duplicating the addition. Reusing the existing function makes the relationship between the old and new capabilities visible.
Describe what the new function accepts and returns.
Make preconditions explicit.
Keep the new task focused and related to the module.
Reuse existing behaviour when it clearly supports the addition.
3. Trace the extension and check what it changes
A trace follows the values through the steps of a function. Start with the stated input, follow each operation in order, and record the result that the code would produce. Check that the result matches the function’s purpose and that the assumptions about the input are satisfied.
After adding a function, consider the module as a whole. The new function should be available alongside the old one, and the old function should still have its original name and role. For a small change, read both functions and trace representative inputs. When the module is part of a larger program, also consider how that program calls the new function and uses its result.
A hand trace is a reasoned prediction, not a report of a program run. If you later test code in an appropriate classroom environment, compare the actual result with the expected result. Do not describe an unrun example as verified.
Follow values through the code in execution order.
Check that the input satisfies the stated preconditions.
Confirm that the new result fits the function’s purpose.
Distinguish a predicted result from an executed test.
4. Practice a small, careful extension
When asked to extend a module, begin by writing one sentence about its current purpose. Then state the new capability and why it belongs there. Identify the existing code you can preserve or reuse, and write the new function so its input and output are clear.
Before considering the work complete, read the whole module again. Check for mismatched names, accidental changes to existing behaviour, unclear assumptions, or a result that is displayed when it should be returned. Use a hand trace to reason through an example input. These checks keep the focus on a well-integrated extension rather than on adding code for its own sake.
For practice, imagine a module that already converts a distance from kilometres to metres. Propose one closely related conversion it could add. State its input and expected result, and explain how you would check that the existing conversion remains available. You do not need to write code to answer.
Describe the current purpose and the proposed addition.
Preserve the module’s existing capability.
Trace a sample input and check the expected result.
Explain how the addition fits the module’s responsibility.
Worked example
Add an average calculation to a score module
An existing score module can total a list of quiz scores. Extend it with a function that returns the average. Preconditions: the list contains at least one numeric score. Input: a list of scores. Algorithm: reuse the total function, then divide the total by the number of scores. Expected output for scores [8, 10, 6]: the average value 8.0. The original total function remains available.
Score module capabilities
The new average function uses the existing total function.
The module already has a function that adds the scores. Keep that function and its purpose unchanged.
Trace the new function
For the stated list, the existing function produces a total of 24. There are three scores, so the new function returns 24 divided by 3, which is 8.0 in Python.
Check the extension
The module now offers both a total and an average. The average function is related to the module’s score-calculation purpose and reuses the existing total calculation.
Answer: The new function returns 8.0 for the stated input. The original total function remains part of the module.
Check: The precondition matters: this version should not be called with an empty list, because the calculation would have no scores to average.
Worked example
Extend a reading module with a summary
An existing reading module counts the number of words in a sentence. Add a function that reports both the word count and the character count, excluding spaces. Preconditions: the input is a string containing at least one word, with words separated by single spaces. Input: a sentence. Algorithm: use the existing word-count function and count non-space characters. Expected output for "read code": 2 words and 8 characters, excluding the space.
Reading module extension
The summary function combines the existing word count with a character count.
Python 3
def word_count(text):
return len(text.split(" "))
def summary(text):
words = word_count(text)
characters = len(text.replace(" ", ""))
return words, characters
# Hand-trace input: "read code"
# Expected result from summary(text): (2, 8)
Keep the original word count
The original function splits the sentence at spaces and returns the number of resulting words. For "read code", that count is 2.
Calculate the added value
The letters in "read" and "code" total eight. The space is not included in the character count requested by the new function.
Return a combined result
The new function returns a pair containing the existing word count and the new character count. The original function remains usable on its own.
Answer: For "read code", the new function returns (2, 8): two words and eight non-space characters.
Check: The precondition specifies single spaces between words. If other spacing is allowed, the module’s rules need to be clarified before relying on this particular implementation.
Worked example
Add a maximum-value function to a temperature module
An existing temperature module converts a list of Celsius readings to Fahrenheit. Extend it with a function that returns the highest Celsius reading. Preconditions: the list contains at least one number. Input: a list of Celsius readings. Algorithm: begin with the first reading, compare each later reading with the current highest, and keep the larger value. Expected output for [17, 21, 19]: 21. The conversion function remains unchanged.
Python 3
def to_fahrenheit(celsius):
return [value * 9 / 5 + 32 for value in celsius]
def highest_celsius(readings):
highest = readings[0]
for value in readings[1:]:
if value > highest:
highest = value
return highest
# Hand-trace input: [17, 21, 19]
# Expected result from highest_celsius(readings): 21
Set the starting value
Use the first reading, 17, as the current highest. This is valid because the precondition guarantees a non-empty list.
Compare later readings
Compare 21 with 17 and keep 21. Then compare 19 with 21 and keep 21. Each comparison updates the current highest only when a larger reading is found.
Return the result
After the list has been considered, the current highest is 21. The new function adds a related summary capability while leaving temperature conversion available.
Answer: The new function returns 21 for [17, 21, 19].
Check: Starting with the first list value avoids inventing an initial reading that could be larger or smaller than the actual temperatures.
Common mistakes and how to avoid them
Replacing or renaming an existing function when the task only asks for an extension.
Correction: Keep the existing capability available, and add the new related function alongside it unless a change is specifically required.
Adding a feature without checking whether it belongs in the module.
Correction: Relate the addition to the module’s purpose. Put unrelated work in a more suitable component.
Leaving input assumptions unstated.
Correction: Name preconditions such as a non-empty list, and decide how allowed inputs should be handled.
Treating a hand trace as proof that code was run.
Correction: Call it an expected result or predicted result. Report an actual test only if the code was genuinely tested.
Lesson summary
Extending a module means adding a related capability to an existing program component.
First identify the module’s purpose and existing behaviour.
State the new function’s preconditions, input, steps, and expected result.
Reuse existing code when it clearly supports the new capability.
Trace examples and check that the original capability remains available.
Check your understanding
Question 1
A module already converts minutes to seconds. Which addition best fits as an extension?
A function that converts hours to seconds.
A function that draws a picture unrelated to time.
A replacement that removes the minutes-to-seconds function.
A function that changes the program’s unrelated login message.
Show answer and explanation
A function that converts hours to seconds.
Converting hours to seconds is a related time conversion. The other choices are unrelated or remove existing behaviour rather than extending it.
Question 2
A new function averages a list by dividing its total by its length. What precondition should be stated for this approach?
The list contains at least one value.
The list contains exactly two values.
Every value is a whole number.
The list is already in alphabetical order.
Show answer and explanation
The list contains at least one value.
The calculation needs a non-zero list length. The other conditions are not required for the described average calculation.
Question 3
A hand trace predicts that a function returns 21 for a sample list. What is the accurate way to report this if the code was not run?
The expected result is 21 for the stated input.
The program was tested and returned 21.
The function is guaranteed to work for every possible input.
The module’s earlier functions have been executed.
Show answer and explanation
The expected result is 21 for the stated input.
A hand trace supports a predicted or expected result, not a claim that the program was executed or that every possible input was checked.
Key terms
Module
A program component that groups related code and can be used as a unit.
Extend
Add a related capability to an existing module while keeping its existing purpose and behaviour in view.
Precondition
A condition that must be true for a function or procedure to be used as intended.
Trace
A step-by-step record of how values change as code is followed for a particular input.
Expected output
The result reasoned from the code and stated input; it does not by itself mean the code was run.
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A2.3. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.