DoAssignment study guide
C1.4 · Design reusable modules and methods
Learn to design reusable modules and methods through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Designing Modular Programs
ICS4U C1.4 · Give shared tasks a clear purpose and interface
What you will learn
- Explain how methods and modules can support reuse.
- Design a method with a clear responsibility, parameters, preconditions, and expected result.
- Choose parameters that allow one method to handle relevant changes in input.
- Trace a method call and describe its expected result without assuming the code was run.
1. From a repeated task to a reusable method
- A method groups instructions around one understandable responsibility.
- Parameters let a method handle different values without copying its instructions.
- A return value makes a computed result available to the caller.
- A module groups related program definitions in a separate unit.
2. Specify the method before writing it
- Specify preconditions, inputs, steps, and expected output.
- Use parameters for values that should vary across calls.
- Make the method name and responsibility agree.
- Group related definitions in a module and keep unrelated responsibilities apart.
3. Trace calls and review the design
- Trace arguments into parameters and the result back to the caller.
- Check whether the method is useful without editing its instructions for each call.
- Design for the variations the task needs.
- A clear name, focused responsibility, and stated assumptions support reuse.
Worked example
Calculate a total
The caller supplies two inputs; the method returns their product.
Python 3
def calculate_total(quantity, unit_price):
return quantity * unit_price
# Illustrative call:
# calculate_total(4, 2.50) returns 10.0- Choose the responsibilityThe method calculates a total and returns it. Keeping display decisions outside the calculation lets a caller use the value in different ways.
- Trace a callFor the illustrative call with quantity 4 and unit price 2.50, the parameters receive those values. Their product is 10.0, the expected returned value. This is a reasoning trace, not a claim that the code was run.
Worked example
Create a status message
Python 3
def make_status_message(name, completed_count):
return name + " completed " + str(completed_count) + " tasks."
# Illustrative call:
# make_status_message("Mina", 3)
# returns "Mina completed 3 tasks."- Separate creation from displayReturning text gives the caller the choice to display it, store it, or use it elsewhere. This focused responsibility makes the method useful in more than one context.
- Trace the inputsFor the illustrative call with name "Mina" and count 3, the returned message is "Mina completed 3 tasks." A different valid name and count produce a corresponding message through the same instructions. These are expected results from reading the method, not reported execution results.
Worked example
Share a method through a module
Two callers use the same method contained in name_tools.
Python 3
# Contents of name_tools.py
def make_display_name(first_name, last_name):
return first_name + " " + last_name
# Illustrative use by either caller:
# name_tools.make_display_name("Ari", "Chen")
# returns "Ari Chen"- Decide what belongs togetherName formatting belongs in a module intended for name-related tools. The callers need that shared service, not separate copies of the formatting instructions.
- Trace a shared callA call with "Ari" and "Chen" supplies those strings to the two parameters and returns "Ari Chen". A second caller can supply different names and use the same method.
Common mistakes and how to avoid them
Lesson summary
- Reusable methods collect a focused task behind a clear name.
- Parameters represent values that vary; return values provide results to callers.
- A module groups related reusable definitions in one shared place.
- State preconditions, inputs, steps, and expected output, then check the design with a trace.
Check your understanding
Question 1
- Store one player's score inside the method and use it for every call.
- Accept the scores as parameters and return the calculated total.
- Copy the calculation into each part of the program that needs it.
- correctIndex
Show answer and explanation
Question 2
- The caller must edit the method's instructions for each new name.
- The caller supplies the method's inputs and can decide what to do with the returned text.
- The method can only be used by the first part of the program that calls it.
- correctIndex
Show answer and explanation
Question 3
- Maintain a separate copy of the rule in each program part.
- Put the related formatting method in one module and have both parts call it.
- Have each program part use a different fixed name.
- correctIndex
Show answer and explanation
Key terms
- Method
- A named group of instructions designed to perform a task and called when that task is needed.
- Module
- A separate, named unit of program code that can hold related definitions for reuse.
- Parameter
- A named input in a method definition that receives a value for a particular call.
- Argument
- A value supplied to a method when it is called.
- Precondition
- A condition expected to be true before a method is called.
- Return value
- A result produced by a method and made available to the caller.
Continue through ICS4U
View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons
Tutor support for C1.4 · Design reusable modules and methods
Working on C1.4 · Design reusable modules and methods in ICS4U? Bring the question and your own attempt. In a live session, you can trace a small example and find the first step where the output differs from what you expected. Ask about the first step that does not make sense, then try a similar question yourself.
Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.
برای مبحث C1.4 · طراحی پیمانهها و روشهای قابل استفاده دوباره در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation C1.4. 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.