DoAssignment study guide

A3.1 · Use existing subprograms appropriately

Learn to use existing subprograms appropriately through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

ICS3U study topic A3.1

A subprogram is a named group of instructions that performs a particular task. An existing subprogram is one that is already available to use, such as a built-in function in a programming language or a function supplied by a library. To use one appropriately, first make sure it does the task you need, then provide the kind of input it expects and use its result correctly. This lesson focuses on that practical choice and use. The examples use Python 3; classrooms may use other languages, where names and syntax can differ.

What you will learn

  • Explain what an existing subprogram does and why a programmer might use one.
  • Choose an existing subprogram that fits a task and check what information it needs and returns.
  • Trace a short program that uses an existing subprogram to predict its output.

Choose a tool that matches the task

When a program needs a value or action, ask whether an existing subprogram already provides it. For example, to find the number of items in a Python list, the built-in function len is a suitable choice. It accepts a sequence such as a list and returns an integer giving its number of items. Using it avoids writing separate instructions to count the items.
A subprogram is not automatically suitable just because its name seems familiar. Check its purpose and the type of input it expects. For a function, also check what kind of result it returns. These details are often described in a language or library reference, course notes, or documentation supplied with the subprogram.
A useful planning routine is: identify the task, select a subprogram whose stated purpose matches, check its required inputs, and decide how the returned result will be used. If the inputs or result do not fit the surrounding program, choose another approach rather than forcing the call.
  • Use an existing subprogram when its purpose fits the task.
  • Check the expected input and returned result before using it.
  • A function call requests a subprogram to work with the supplied input.

Inputs, processing, and results

A subprogram call can be understood as input, process, and result. The input is the information supplied in the call. The process is the work performed by the existing subprogram. The result is the value it returns, if it returns one. A program may store that result in a variable, use it in a calculation, or display it.
In Python 3, the parentheses in a function call hold the input information, often called an argument. For example, len(items) passes the variable items to len. The returned count can be assigned to a variable. Python list indexes start at zero, but len returns the number of items, not the last index.
Some existing subprograms return a value; others may perform an action such as displaying information. Know which kind you are using. Do not assume a call changes the original data or displays a result unless its documented purpose says so.
  • An argument is information supplied to a subprogram call.
  • A returned value can be saved or used in another expression.
  • Read the stated behaviour; do not infer extra effects.

Use documentation and check the fit

Before using a subprogram, find enough information to answer three questions: What task does it perform? What input does it accept? What does it return or do? Documentation may also show a short example. Compare the documented behaviour with the exact need in your program.
A mismatch can cause an error or an incorrect result. For example, a function that expects text may not be the right choice for a list of numbers. A function that returns a rounded value may not be suitable if the task requires the original precision. When several existing subprograms seem relevant, choose the one whose documented behaviour most directly matches the requirement.
After selecting a subprogram, trace a small example by hand. Write down the input, the value returned, and how the rest of the program uses it. This helps catch misunderstandings before relying on the result. The code in this lesson is shown as text and is not executed.
  • Documentation helps confirm purpose, input, and result.
  • A suitable subprogram must meet the task's actual requirement.
  • Tracing a small input shows how the returned value affects later steps.

Practice the selection routine

For each task, state what information is available and what result is needed. Then name an existing subprogram only if you can explain why its documented purpose fits. For instance, if the task is to find the smallest value in a list of numbers, Python's built-in min is a reasonable candidate: it accepts the list and returns its smallest value.
Before writing a call, check that the supplied information has the expected form. After writing it, trace the result and make sure the program uses it for the intended purpose. If the task requires a different kind of result, reconsider the choice.
In another classroom language, the available subprograms and their spellings may differ. The reasoning remains the same: identify the task, check the available subprogram's purpose and inputs, then use its result according to its documented behaviour.
  • State the needed result before choosing a subprogram.
  • Match the available input to what the subprogram expects.
  • Trace the returned value through the program.

Trace the values passed to existing subprograms

ExampleInput to callReturned valueVariable used
Count entriesreadings: three list items3total
Find smallest scorescores: 74, 88, 69, 9169lowest
Round shareapproximately 3.3333 and 2 decimal places3.33share

Worked example

Count the entries in a list

A program has a list of three temperatures and needs to report how many entries it contains. Use an existing subprogram rather than counting by hand.
List passed to len
Index → stored value018121219

Python 3 list indexes start at zero; len returns the number of entries.

Python 3

readings = [18, 21, 19]
total = len(readings)
print(total)
  1. Choose the subprogram
    Python 3's built-in len function returns the number of items in a list. Its input is the list named readings, so it fits the stated task.
  2. Trace the call
    The list contains 18, 21, and 19. The call len(readings) therefore returns 3. Assigning the result stores that count in total.
  3. Predict the display
    The print call displays the value stored in total. This result follows from tracing the list and function call; the code has not been executed.
Answer: The expected output is 3.
Check: There are three list entries. In Python, the list's zero-based indexes are 0, 1, and 2, but len reports the count, 3.

Worked example

Find the lowest score

A teacher's practice program has a list of four scores and needs to display the smallest one. Select an existing subprogram that provides this result.
Scores supplied to min
Index → stored value074188269391

Python 3 indexes are zero-based; min returns a value, not an index.

Python 3

scores = [74, 88, 69, 91]
lowest = min(scores)
print(lowest)
  1. Match the task
    Python 3's built-in min function returns the smallest value among the values in a list. The input here is a list of numbers, so its purpose fits the task.
  2. Follow the returned value
    The list contains 74, 88, 69, and 91. Comparing these values, the smallest is 69. The call min(scores) returns that value, which is assigned to lowest.
  3. Trace the output
    The print call displays lowest, so the predicted output is 69. The result is derived by comparing the four supplied values, not by claiming the code was run.
Answer: The expected output is 69.
Check: The input is a list of numeric scores; min returns the lowest value, not its position in the list.

Worked example

Round a calculated amount

A program calculates a per-person share and needs to display it to two decimal places. Use an existing subprogram for rounding, and trace the value passed to it.
Calculate, round, display
approximately 3.33333.33Calculate 10.00 / 3round(value, 2)Display share

The calculated value is passed to round; its returned value is displayed.

Python 3

share = round(10.00 / 3, 2)
print(share)
  1. Check the function's purpose
    Python 3's built-in round function takes a number and a number of decimal places, and returns a rounded value. Those inputs fit the display requirement in this example.
  2. Trace the calculation
    The value of 10.00 divided by 3 is approximately 3.3333. Passing that value and 2 to round requests rounding to two decimal places, giving 3.33.
  3. Use the result
    The rounded result is assigned to share and then displayed. This example uses round to produce a numeric value; a different display requirement may need a different documented subprogram.
Answer: The expected displayed value is 3.33.
Check: The call supplies both the number to round and the requested number of decimal places. The result is a number rounded to two decimal places.

Common mistakes and how to avoid them

Choosing a function because its name sounds relevant, without checking its behaviour.
Correction: Use documentation or course materials to confirm the purpose, input, and returned result.
Treating the result of len as the last list index.
Correction: len gives the number of items. For a list of three items, the Python 3 indexes are 0, 1, and 2, while len returns 3.
Assuming a subprogram changes the input or displays a value automatically.
Correction: Use its documented behaviour. If it returns a value, save it or pass it where needed; do not assume additional effects.
Supplying an input that does not match what the subprogram expects.
Correction: Check the input requirements before making the call, and choose a subprogram that fits the available data.

Lesson summary

  • An existing subprogram is a named piece of code already available for use.
  • Use one when its documented purpose matches the task.
  • Check its inputs and returned result, then trace how the program uses that result.
  • The examples use Python 3; other classroom languages may have different subprogram names and syntax.

Check your understanding

Question 1

A Python 3 program needs the number of items in a list. Which choice best fits?
  1. Use len on the list and store its returned count.
  2. Use min on the list and treat the smallest value as the count.
  3. Use round on the list and treat the result as the count.
  4. correctIndex подкомментарий
Show answer and explanation
Use len on the list and store its returned count.
len returns the number of items in a list. min returns a smallest value, and round is for rounding a number.

Question 2

A list contains 12, 7, and 15. What value does Python 3's min function return for this list?
  1. 3, because there are three items
  2. 7, because it is the smallest listed value
  3. 2, because it is the final zero-based index
  4. correctIndex”: 1
Show answer and explanation
7, because it is the smallest listed value
min returns the smallest value, 7. The list has three entries and its final Python 3 index is 2, but neither is the result of min.

Key terms

Subprogram
A named group of instructions that performs a particular task.
Existing subprogram
A subprogram already available in a programming language or library.
Argument
Information supplied to a subprogram when it is called.
Return value
A value produced by a function and made available to the calling code.
Documentation
Information describing how a subprogram is intended to be used, including its purpose and inputs.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A3.1. 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