DoAssignment study guide

C1.3 · Decompose a task into subprograms

Learn to decompose a task into subprograms through clear examples and targeted practice.

Ontario Grade 12 Computer Science

Designing Modular Programs

ICS4U study topic C1.3: Divide a programming task into clear, connected parts

In ICS3U, you learned to plan programs as instructions that produce an intended result. When a task has several actions, it can be easier to understand by dividing it into named parts. This is decomposition: identifying smaller jobs that work together to complete the whole task. A subprogram is one named part with a particular responsibility. In this lesson, you will practise choosing those responsibilities and describing how the parts connect. Python functions are one way to represent subprograms; the planning idea applies in other programming languages too.

What you will learn

  • Explain how decomposition divides a programming task into subprograms.
  • Choose useful subprograms with clear responsibilities.
  • Identify what information each subprogram needs and what result it provides.
  • Trace how a main part coordinates subprograms to complete a task.

1. Start with the whole task

Before deciding on subprograms, describe what the complete program must do. Identify its input and its expected outcome. For example, a purchase-cost task might receive an item price and a tax rate, calculate the tax, add it to the price, and present the total. Describing the whole job first gives you a way to check whether your smaller parts work together.
Look for actions that form distinct, useful jobs. In the purchase-cost task, calculating the tax and calculating the final cost are separate calculations. A main part can coordinate them: it supplies information, receives results, and connects the jobs. The main part still serves the overall task; it makes the sequence of work visible.
The goal is not to split every instruction into its own subprogram. Too many tiny parts can make the task harder to follow. Nor should one part collect unrelated jobs without a clear responsibility. Choose parts that make the solution easier to understand while still covering the actions the complete task needs.
  • State the complete task's input and expected outcome first.
  • Group related actions into meaningful jobs.
  • Use a level of detail that clarifies the task rather than fragmenting it.

2. Give each part a responsibility and a hand-off

A subprogram's name and description should help a reader predict its job. A name such as calculate_tax communicates more than do_work. A clear responsibility helps you decide what belongs in that subprogram and what should remain elsewhere.
For each part, identify the information it needs and the result it provides. In the purchase-cost task, calculate_tax needs a price and a rate and provides a tax amount. A separate final_cost part needs the price and tax amount and provides the total. These descriptions let you check that each part has enough information to do its job.
The main part connects the hand-offs. It can call calculate_tax, keep the returned amount, and supply that amount with the price to final_cost. Then it can use the returned total for the required outcome. If a part's result is not used, or the next part lacks information it needs, revise the plan before writing code.
  • Give each subprogram a clear responsibility.
  • Identify the information supplied to it and the result it provides.
  • Check that the main part connects the jobs and uses their results.

3. Represent, trace, and review a decomposition

Python 3 functions can represent subprograms. A function may receive information, perform its assigned job, and return a result for the main part to use. Read a decomposed solution by following the main part's calls in order: note what information is supplied, what result comes back, and where that result goes next.
A trace is a reasoned walk-through of the displayed code. It is useful for checking whether the design completes the task as described. State any assumptions needed to interpret the input, follow the values through each part, and derive the expected output. A trace does not mean the code has been run.
When reviewing a plan, check that the responsibilities are understandable, the required actions are covered, and every subprogram receives the information it needs. Check also that results are used for the overall outcome. For practice, consider a program that receives a distance in kilometres and displays metres. One suitable plan has a conversion subprogram that receives the distance and returns the converted value; the main part receives the distance and presents the returned value.
  • Trace calls and returned results in the order the main part uses them.
  • Check the trace against the whole task's expected outcome.
  • Revise unclear responsibilities or missing hand-offs.

Worked example

Plan a purchase-cost task

A program receives an item price and a tax rate and must show the cost including tax. Propose subprograms and describe how they fit together.
Purchase-cost subprograms
price and ratetax amountprice and tax amounttotalMain: receive input andcalculate_tax: return tafinal_cost: return total

The main part coordinates two calculation subprograms and uses their returned results.

Python 3

def calculate_tax(price, rate):
    return price * rate

def final_cost(price, tax_amount):
    return price + tax_amount

price = 20.00
rate = 0.08
tax_amount = calculate_tax(price, rate)
total = final_cost(price, tax_amount)
print(total)
  1. State the whole task
    The input is an item price and a tax rate. The expected outcome is the cost after tax has been added. The rate is assumed to be provided as a decimal, such as 0.08 for eight percent.
  2. Assign clear jobs
    The main part receives the input and coordinates two subprograms. calculate_tax receives the price and rate and returns the tax amount. final_cost receives the price and tax amount and returns the total.
  3. Check the hand-offs
    The main part supplies the returned tax amount to final_cost. Both calculation jobs contribute to the requested outcome, and each has a distinct responsibility.
Answer: A suitable decomposition has a main part that coordinates calculate_tax and final_cost, then presents the returned total.
Check: The plan identifies each part's responsibility, the information it needs, and how its result contributes to the whole task.

Worked example

Trace the purchase-cost program

For a price of 20.00 and a tax rate of 0.08, trace the functions and derive the expected displayed value.
Purchase-cost result hand-offs
price and ratetax amount 1.60price and tax amountreturned total 21.60Main: price 20.00, ratecalculate_tax: returns 1final_cost: returns 21.6Main: display 21.6

The main part passes the input to calculate_tax, then passes the returned tax amount to final_cost.

Python 3

def calculate_tax(price, rate):
    return price * rate

def final_cost(price, tax_amount):
    return price + tax_amount

price = 20.00
rate = 0.08
tax_amount = calculate_tax(price, rate)
total = final_cost(price, tax_amount)
print(total)
  1. State assumptions and input
    Assume the price is non-negative and the rate is given as a decimal. The supplied values are a price of 20.00 and a rate of 0.08.
  2. Follow the first hand-off
    The main part calls calculate_tax with the price and rate. Multiplying 20.00 by 0.08 gives 1.60, which the function returns as the tax amount.
  3. Follow the next result
    The main part calls final_cost with 20.00 and 1.60. Adding them gives 21.60. The print instruction displays the numeric value 21.6; it does not require two decimal places.
Answer: The trace gives a tax amount of 1.60 and a total of 21.60. The expected numeric display is 21.6.
Check: The result from calculate_tax is passed to final_cost, which returns the value used for the final display.

Worked example

Decompose a quiz-average task

A program receives three quiz scores and displays their average. Propose a decomposition, then trace scores 6, 8, and 10.
Quiz-average subprogram
three scoresaverage resultMain: receive scores andcalculate_average: retur

The main part supplies the scores and uses the average returned by the calculation subprogram.

Python 3

def calculate_average(score1, score2, score3):
    total = score1 + score2 + score3
    return total / 3

average = calculate_average(6, 8, 10)
print(average)
  1. Choose responsibilities
    The main part receives the three scores and coordinates the result. A calculate_average subprogram receives the scores, calculates their average, and returns it.
  2. Trace the supplied values
    The subprogram adds 6, 8, and 10 to get 24. It divides this total by the three scores, returning 8.0.
  3. Connect to the outcome
    The main part uses the returned average for display. The expected numeric output is 8.0. Receiving and presenting are distinct from the calculation responsibility.
Answer: Use the main part to receive and display, and calculate_average to calculate and return the average. For these scores, the returned value is 8.0.
Check: The calculation has a clear responsibility, and the main part uses its result to complete the task.

Common mistakes and how to avoid them

Using a vague name such as do_work.
Correction: Choose a name that communicates the subprogram's responsibility, such as calculate_tax.
Putting several unrelated jobs into one subprogram.
Correction: Separate distinct jobs when doing so makes each part easier to understand and connect.
Leaving out information a subprogram needs or ignoring its result.
Correction: State the information supplied to each part and the result it provides, then check how the main part uses them.
Making every tiny instruction a separate subprogram.
Correction: Choose meaningful, manageable jobs rather than splitting the task so far that its overall purpose becomes hard to follow.

Lesson summary

  • Decomposition divides an overall programming task into smaller subprograms that work together.
  • Start with the whole task, then give useful parts clear responsibilities.
  • Identify the information each subprogram needs and the result it provides.
  • Trace the main part's hand-offs to check that the parts complete the intended task.

Check your understanding

Question 1

A program must convert a distance and then display the result. Which design best shows decomposition?
  1. Use a conversion subprogram that returns the converted value, and let the main part display it.
  2. Put every instruction in one sequence and name the sequence do_everything.
  3. Create a display subprogram that does not receive or use the converted value.
  4. correctIndex
Show answer and explanation
Use a conversion subprogram that returns the converted value, and let the main part display it.
The conversion subprogram has a clear job and returns a value the main part can use for the display.

Question 2

In the purchase-cost example, what result does calculate_tax provide for a price of 20.00 and a rate of 0.08?
  1. 1.60, the tax amount
  2. 20.08, the final cost
  3. 21.60, the tax amount
  4. correctIndex
Show answer and explanation
1.60, the tax amount
Multiplying 20.00 by 0.08 gives a tax amount of 1.60. The final cost is calculated afterward.

Question 3

What is a useful check when deciding whether a task has been decomposed clearly?
  1. Each subprogram has a clear responsibility and the information it needs.
  2. Every subprogram has the same responsibility.
  3. The main part never uses results from subprograms.
  4. correctIndex
Show answer and explanation
Each subprogram has a clear responsibility and the information it needs.
Clear responsibilities and suitable hand-offs help the parts work together to complete the overall task.

Key terms

Decomposition
Dividing an overall task into smaller parts that work together to complete it.
Subprogram
A named part of a program responsible for a particular job.
Input
Information supplied to a program or subprogram so it can do its job.
Result
A value or outcome a subprogram provides for use by another part.

Continue through ICS4U

View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons

Tutor support for C1.3 · Decompose a task into subprograms

Working on C1.3 · Decompose a task into subprograms in ICS4U? Bring the question and your own attempt. In a live session, you can trace a small input step by step, compare expected and actual output, and test an edge case. 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.3 · شکستن کار به زیر‌برنامه‌ها در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه می‌توانید یک ورودی کوچک را گام‌به‌گام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامه‌نویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقه‌ای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی می‌شود.

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation C1.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.

Official curriculum reference

Report a correction or ask a question