DoAssignment study guide
A3.2 · Write subprograms with parameters and appropriate scope
Learn to write subprograms with parameters and appropriate scope through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Programming Concepts and Skills
ICS3U study topic A3.2
What you will learn
- Describe how a subprogram helps organize a program into named tasks.
- Write a Python 3 subprogram that receives information through parameters.
- Explain how a parameter and other local variables are used within a subprogram.
- Choose variable scope that keeps information available only where it is needed.
A named task receives information
- A parameter is a name in a subprogram definition.
- A call supplies values that the parameters receive.
- A returned value can be used by the calling code.
Scope keeps names in the right place
- A parameter’s name can be used inside its subprogram.
- A local variable is for work within its subprogram.
- Pass needed information in and return needed results out.
Read, trace, and check a subprogram
- Match call values to parameters in order.
- Trace local calculations separately from the calling code.
- Check that a needed result is returned.
Build the habit with a small task
- Plan inputs and results before writing instructions.
- Use parameter names to make information needs clear.
- Keep temporary working names local to the task.
Trace of add_five(8)
| Point in the trace | start | result | answer |
|---|---|---|---|
| Call supplies 8 | 8 | not assigned yet | not assigned yet |
| Subprogram calculates | 8 | 13 | not assigned yet |
| Returned value assigned | subprogram finished | local to subprogram | 13 |
Worked example
Calculate a line total
The call supplies two values; the named task returns their product.
Python 3
def line_total(unit_price, quantity):
total = unit_price * quantity
return total
answer = line_total(4, 3)- Identify the inputsThe task needs a unit price and a quantity, so the definition has one parameter for each. The call supplies 4 first and 3 second, matching the parameter order.
- Follow the calculationInside the subprogram, the parameter values are used to calculate a total. The subprogram returns that total to the calling code.
- Trace the resultFor this call, unit_price receives 4 and quantity receives 3. The product is 12, so the call provides 12 as its result.
Worked example
Use a local variable for a calculation
The parameter and temporary result are used inside the subprogram; the return value goes back to the caller.
Python 3
def add_five(start):
result = start + 5
return result
answer = add_five(8)- Match the inputThe call supplies 8, so the parameter start receives 8. The parameter is available inside add_five.
- Track the local valueThe assignment creates result inside add_five. It stores the value of start plus 5. Both start and result are used within this subprogram.
- Return the valueThe subprogram returns result, which is 13. The calling code assigns that returned value to answer. result is not the name used to store the answer in the calling code.
Worked example
Pass each subprogram the information it needs
The caller passes a name into the subprogram and receives its returned message.
Python 3
def make_greeting(person_name):
message = "Hello, " + person_name + "!"
return message
name_text = "Mina"
greeting = make_greeting(name_text)- Choose the parameterThe greeting task needs a name, so its parameter is person_name. The caller passes name_text as the value for that parameter.
- Form the greetingThe subprogram combines the greeting text with the received name. The local variable message stores the greeting while the subprogram is working.
- Follow the returnFor the stated call, person_name receives Mina and message becomes Hello, Mina!. The returned value is assigned to greeting in the calling code.
Common mistakes and how to avoid them
Lesson summary
- A subprogram is a named task that can receive information and provide a result.
- Parameters name the information a subprogram receives; calls supply values for those parameters.
- Parameters and local working variables are used within the subprogram’s scope.
- Return a result when the calling code needs it, and choose scope so names are available only where needed.
Check your understanding
Question 1
- A parameter name available inside show_limit
- A value that must always be 0
- A local variable in every part of the program
- The returned result, even if there is no return statement
Show answer and explanation
Question 2
- Return the value so the calling code can receive it
- Assume total is automatically available everywhere
- Rename the parameter to total
- Remove the calculation
Show answer and explanation
Question 3
- 3
- 6
- 8
- 12
Show answer and explanation
Key terms
- Subprogram
- A named set of instructions that performs a task and can be called from another part of a program.
- Parameter
- A name in a subprogram definition that receives a value supplied by a call.
- Call
- An instruction that asks a subprogram to perform its task, usually supplying values it needs.
- Scope
- The part of a program where a variable name can be used.
- Local variable
- A variable name used for work within a subprogram.
- Return value
- The result a subprogram sends back to the calling code.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
- A3.1 · Use existing subprograms appropriately
- A4.1 · Find and correct syntax, logic, and run-time errors
- 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.2. 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.