Learn to design reusable program parts through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Software Development
ICS3U study topic B2.3
A reusable program part is a named section of a program that carries out a task and can be used again. Instead of rewriting the same instructions wherever a task is needed, a programmer can give those instructions one clear name and use that name when needed. This lesson focuses on designing small, understandable parts that can serve more than one situation. Examples use Python 3, but classroom languages may use different words and punctuation for the same ideas.
What you will learn
Explain how a named program part can make a task reusable.
Choose inputs and an output for a small reusable task.
Trace a call to a program part and describe the result.
Recognize how a program can use the same part for different data.
Start with a task that can be reused
Suppose a school club program needs to display a short welcome message for several members. If the message instructions are copied repeatedly, changing the wording means finding and editing every copy. A reusable part keeps the instructions in one place. The main program can ask that part to do the task for each member.
Before writing a part, describe its responsibility in one sentence. For example: “Make a welcome message from a member’s name.” This description helps keep the part focused. A part that tries to welcome someone, calculate a fee, and print a schedule has several responsibilities and is harder to reuse for just one of them.
Think through input, process, and output. Input is the information the part needs; process is the work it performs; output is the result it provides or displays. Some parts produce a value for the rest of the program to use. Other parts carry out an action such as displaying a message. Decide which kind suits the task.
Give a reusable part one clear task.
Identify what information it needs and what result or action it provides.
A clear name helps a reader understand when to use the part.
Design the part and its use
In Python 3, a function is a named program part. A parameter is a name in the function definition for information the function will receive. When the program calls the function, it supplies an argument: the actual value for that use. A return statement sends a result back to the calling code. These are Python terms; another classroom language may express the same ideas differently.
Use names that tell the reader what the part does and what its information represents. For a function that creates a welcome message, a name such as make_welcome is more informative than x. Its parameter could be member_name. When the function returns a message rather than displaying it itself, the calling code can decide what to do with that result.
To check a design, trace one call by hand: note the argument, match it to the parameter, follow the instructions, and record the returned value. Then consider another suitable argument. If the same instructions still make sense, the part is serving different data without needing a copied version.
A parameter names information a function receives; an argument is the value supplied in a call.
A returned result can be used by the code that made the call.
Trace the information into the part and its result back out.
Keep reuse understandable
Reuse is not simply putting any instructions into a function. The part should have a clear purpose and a useful boundary: the calling code should be able to provide the needed information and understand what comes back or what action occurs. A helpful design keeps the task inside the part and leaves decisions about using its result to the caller when that makes the part useful in more than one place.
A reusable part should not depend on a particular example value being built into its instructions when that value is expected to vary. For instance, a function intended to welcome different members should receive each member’s name. If the name is fixed inside the function, using it for another member would require changing the function instead of supplying new information.
Start with a small, concrete task and test the design on two different examples by tracing them. This is a reasoning check, not a claim that code was run. If the inputs and results are easy to follow, the part is easier for another programmer to use and maintain.
Supply changing information through inputs rather than fixing it unnecessarily inside the part.
Keep the task and the expected result clear to the caller.
Trace more than one call to check whether the part is genuinely reusable.
Practice the design process
For a new task, write a one-sentence purpose, list the information needed, and decide whether the part should return a value or perform an action. Choose a name that reflects the purpose, then trace a call using a concrete value. Finally, try a second value and check that the same part still fits.
Practice: a classroom program needs to calculate the number of points earned from a correct answer, where the points depend on the question’s point value. Describe a reusable part for this task. Identify its input and result, and explain how two questions could use the same part. Do not add unrelated tasks to the part.
Purpose → inputs → result or action → trace calls.
Use the same reasoning process before writing code in any classroom language.
Hand trace: welcome function calls
Call
Argument assigned to member_name
Returned value
make_welcome("Mina")
Mina
Welcome, Mina!
make_welcome("Joel")
Joel
Welcome, Joel!
Worked example
Return a welcome message
Design a part that receives a member’s name and returns a welcome message. Trace calls using the names Mina and Joel.
Caller and reusable function
A name enters the function; its returned message goes back to the caller.
Python 3
def make_welcome(member_name):
return "Welcome, " + member_name + "!"
first = make_welcome("Mina")
second = make_welcome("Joel")
Set the purpose and input
The part’s single task is to build a welcome message. It needs one name as input and returns the completed text, so the caller can display it or use it elsewhere.
Trace the first call
In Python 3, the argument "Mina" is assigned to the parameter member_name. The return expression combines the fixed greeting with that parameter. The caller receives the resulting message.
Check reuse
For the Joel call, only the supplied argument changes. The same instructions form a message with Joel’s name, showing that the part is not tied to Mina.
Answer: The two expected returned messages are "Welcome, Mina!" and "Welcome, Joel!".
Check: The code below is Python 3. The calls provide the inputs; the function returns a string and does not print it. Tracing each argument through the return expression gives the stated messages. Python uses zero-based indexes for sequences, but no sequence indexes are used here.
Worked example
Return a total from two inputs
A club program needs to find a member’s total points from two recorded amounts. Design a reusable part and trace it for 12 and 7 points.
Inputs produce a returned total
Two amounts are supplied to one focused part, which returns their total.
The total depends on two amounts, so the function receives two parameters. The values 12 and 7 are the arguments for this example.
Follow the calculation
The function adds the two supplied values and returns the sum. Tracing the call gives 12 plus 7, which is 19; the caller stores that returned value in total.
Check the task boundary
The function only finds the total. It does not decide what the points mean or display a report. This focused task can be called again with other point amounts.
Answer: The call returns 19, so total has the value 19.
Check: This Python 3 example receives two numbers and returns their sum. By hand, substitute 12 for first_amount and 7 for second_amount; the returned value is 19. The code is traced, not reported as executed.
Worked example
Reuse a part that displays an action
A classroom program needs to show a reminder for different activities. Design a part that receives an activity name and displays a reminder. Trace calls for "reading" and "sketching".
One action used with two inputs
Each call supplies an activity name to the same reminder part.
This part’s job is to display text. It receives the activity name as input and uses print to show a reminder. Unlike the earlier examples, it is not designed to return a message for the caller to store.
Trace the first call
The first argument, "reading", becomes the value of activity. The displayed line is Reminder: reading. The function’s instructions then finish.
Trace the second call
The second call supplies "sketching" to the same parameter. It displays Reminder: sketching. The reminder wording stays in one place while the activity changes between calls.
Answer: The calls display Reminder: reading and Reminder: sketching, in that order.
Check: This Python 3 function takes one string input and displays a line. It has no return statement, so the intended result is the displayed action rather than a returned message. These outputs follow from tracing the two calls; the code was not executed.
Common mistakes and how to avoid them
Writing a separate copy of the instructions for every name or activity.
Correction: Use one part with an input for the information that changes, then call it with each needed value.
Confusing a parameter with the value supplied in a call.
Correction: The parameter is the receiving name in the definition; the argument is the value supplied for a particular call.
Assuming every function must return a value.
Correction: Choose the design that fits the task: a part may return a result, or it may carry out an action such as displaying a line.
Giving one reusable part several unrelated jobs.
Correction: Keep its purpose focused so a caller can understand when to use it and what to expect.
Lesson summary
A reusable program part is a named set of instructions designed for use in more than one situation.
Design it by stating its task, identifying needed inputs, and deciding whether it returns a result or performs an action.
Use clear names and trace arguments through the part to check its expected result.
Try more than one suitable input to see whether the same part can serve both calls.
Check your understanding
Question 1
A function definition uses the parameter activity. In the call show_reminder("reading"), what is "reading"?
The argument supplied to the call
The function’s returned value
The parameter name in the definition
A second function
Show answer and explanation
The argument supplied to the call
"reading" is the value supplied in this call, so it is the argument. The parameter is the receiving name activity.
Question 2
A part receives two amounts, adds them, and returns the total. What is the most useful description of its task?
Add the two supplied amounts and return their sum
Display every detail about the member
Store one fixed total for all calls
Ask the user to choose a new activity
Show answer and explanation
Add the two supplied amounts and return their sum
This description matches the two inputs and returned result while keeping the part focused.
Question 3
A message must be made for several different names. Which design best supports reuse?
Put one name directly into the message instructions and copy them for every person
Receive the name as input and use it to make the message
Use a different function name for each person
Leave out the name and return the same fixed message every time
Show answer and explanation
Receive the name as input and use it to make the message
Passing the changing name as input lets the same instructions create a message for each person.
Key terms
Reusable program part
A named section of instructions designed to carry out a task and be used again.
Function
In Python 3, a named program part that can receive information and may return a result.
Parameter
A name in a function definition that receives information for a call.
Argument
A value supplied when a function is called.
Return
Send a result from a function back to the code that called it.
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation B2.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.