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
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
- 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
- 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
- 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
- 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
| Example | Input to call | Returned value | Variable used |
|---|---|---|---|
| Count entries | readings: three list items | 3 | total |
| Find smallest score | scores: 74, 88, 69, 91 | 69 | lowest |
| Round share | approximately 3.3333 and 2 decimal places | 3.33 | share |
Worked example
Count the entries in a list
Python 3 list indexes start at zero; len returns the number of entries.
Python 3
readings = [18, 21, 19]
total = len(readings)
print(total)- Choose the subprogramPython 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.
- Trace the callThe list contains 18, 21, and 19. The call len(readings) therefore returns 3. Assigning the result stores that count in total.
- Predict the displayThe print call displays the value stored in total. This result follows from tracing the list and function call; the code has not been executed.
Worked example
Find the lowest score
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)- Match the taskPython 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.
- Follow the returned valueThe 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.
- Trace the outputThe 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.
Worked example
Round a calculated amount
The calculated value is passed to round; its returned value is displayed.
Python 3
share = round(10.00 / 3, 2)
print(share)- Check the function's purposePython 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.
- Trace the calculationThe 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.
- Use the resultThe 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.
Common mistakes and how to avoid them
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
- Use len on the list and store its returned count.
- Use min on the list and treat the smallest value as the count.
- Use round on the list and treat the result as the count.
- correctIndex подкомментарий
Show answer and explanation
Question 2
- 3, because there are three items
- 7, because it is the smallest listed value
- 2, because it is the final zero-based index
- correctIndex”: 1
Show answer and explanation
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
- A2.2 · Use sequence, selection, and repetition
- A3.2 · Write subprograms with parameters and appropriate scope
- A1.1 · Use variables and constants with appropriate data types
- A1.2 · Explain binary, hexadecimal, and character encoding
- A1.3 · Use arithmetic and string assignment statements
- A1.4 · Use Boolean, comparison, and arithmetic operators correctly
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.