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

A subprogram is a named set of instructions for a task. Instead of repeating the same instructions, a program can call the subprogram and provide the information it needs. The names in its parameter list are placeholders for that information. Scope means the part of a program where a variable name can be used. In this lesson, you will practise giving a subprogram the information it needs and keeping its working variables in an appropriate scope. Examples use Python 3; classrooms may use other languages, whose syntax may differ.

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

Consider a program that calculates the cost of several items. A subprogram can handle the repeated task of multiplying an item’s price by its quantity. Its parameters let the same set of instructions work with different values.
In Python 3, a subprogram is written with def, followed by its name and a parameter list. When the program calls it, the supplied values are received under the parameter names. The subprogram’s instructions use those names. A return statement can send a calculated result back to the calling part of the program.
Think of the process as input, process, and output: the call supplies input values; the subprogram processes them; and the return value provides output. Parameters describe what information the task needs. They are not the same as the particular values supplied at a call.
  • 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 variable’s scope is where its name is available to use. A parameter is available inside the subprogram that lists it. A variable assigned inside a subprogram is normally local to that subprogram: its name is used there, not as a general name throughout the program.
For an introductory program, prefer passing a value the subprogram needs as a parameter and returning a result when the calling code needs it. This makes the information flow visible. It also avoids relying on a variable that happens to be defined elsewhere.
Choose a clear parameter name that describes the information received. Use local names for temporary working values that are needed only for the subprogram’s task. Keep each subprogram focused on the task its name describes.
  • 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

When reading a call, match supplied values to parameters from left to right. Then follow the subprogram’s instructions using those received values. If it creates a local variable, track that value within the subprogram. Finally, follow the return statement to see what value the call provides.
A trace table can show how values change or move through a subprogram. Write down the parameter values for a particular call, any local calculation, and the returned result. This is a way to reason about the code; the examples below are traced by hand and are not reported as executed.
Before using a subprogram, check that the call provides the information its parameter list requires, that the instructions use the parameters for the intended task, and that any needed result is returned. Also check that a name is being used within its scope.
  • 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

For a new task, first state what information it needs and what result it should provide. Give those inputs parameter names, write the task using those names, and return the result if the calling code needs it. Keep working variables local unless there is a clear reason they need a wider scope.
To practise, describe a subprogram that converts a number of minutes into hours and leftover minutes. Identify the input parameter and the result the caller would need. Then describe a second subprogram that takes a name and returns a greeting. For each, identify one local value if the calculation needs one.
This planning approach applies in Python 3 and in other classroom languages, even though their definitions, calls, and return syntax may look different.
  • 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 tracestartresultanswer
Call supplies 88not assigned yetnot assigned yet
Subprogram calculates813not assigned yet
Returned value assignedsubprogram finishedlocal to subprogram13

Worked example

Calculate a line total

Write a subprogram that receives a unit price and quantity, then returns their product. Trace a call with price 4 and quantity 3.
Values flow through the subprogram
price and quantityproductCall: 4, 3line_total(unit_price, qReturned result: 12

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)
  1. Identify the inputs
    The 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.
  2. Follow the calculation
    Inside the subprogram, the parameter values are used to calculate a total. The subprogram returns that total to the calling code.
  3. Trace the result
    For this call, unit_price receives 4 and quantity receives 3. The product is 12, so the call provides 12 as its result.
Answer: The subprogram returns 12 for the stated call.
Check: The answer follows from the supplied values: 4 multiplied by 3 is 12.

Worked example

Use a local variable for a calculation

A subprogram receives a starting number and adds 5 before returning the result. Trace a call with starting number 8, and identify the scope of the names.
Local work and returned result
parameter valuereturn valueCaller: supplies 8add_five: start, resultCaller: answer receives

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)
  1. Match the input
    The call supplies 8, so the parameter start receives 8. The parameter is available inside add_five.
  2. Track the local value
    The assignment creates result inside add_five. It stores the value of start plus 5. Both start and result are used within this subprogram.
  3. Return the value
    The 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.
Answer: The call returns 13, which is assigned to answer.
Check: The parameter has value 8, and adding 5 gives 13. The local name result holds that value during the subprogram’s work.

Worked example

Pass each subprogram the information it needs

A program stores a person’s name in name_text. Write a subprogram that receives a name and returns a greeting. Trace the call using the name Mina.
A name becomes a returned greeting
name valuereturned messagename_text: Minamake_greeting(person_namgreeting: Hello, Mina!

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)
  1. Choose the parameter
    The greeting task needs a name, so its parameter is person_name. The caller passes name_text as the value for that parameter.
  2. Form the greeting
    The subprogram combines the greeting text with the received name. The local variable message stores the greeting while the subprogram is working.
  3. Follow the return
    For the stated call, person_name receives Mina and message becomes Hello, Mina!. The returned value is assigned to greeting in the calling code.
Answer: The call returns the text Hello, Mina! and assigns it to greeting.
Check: The parameter receives the supplied name, and the returned message includes that name.

Common mistakes and how to avoid them

Using a parameter name as if it were a value supplied by the caller.
Correction: The parameter is a placeholder name in the definition; the call supplies the particular value it receives.
Assuming a local variable inside a subprogram can be used directly by the calling code.
Correction: Return the needed result, then assign the returned value to a name in the calling code.
Writing a subprogram that needs information but does not receive it as a parameter.
Correction: List the required information as parameters and provide corresponding values at the call.
Supplying values in a different order from the parameter list.
Correction: Match each supplied value to its parameter in order, and use a clear parameter list.

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

In Python 3, what is limit in def show_limit(limit):?
  1. A parameter name available inside show_limit
  2. A value that must always be 0
  3. A local variable in every part of the program
  4. The returned result, even if there is no return statement
Show answer and explanation
A parameter name available inside show_limit
limit is listed in the parameter list, so a call supplies a value that the subprogram receives under that name.

Question 2

A subprogram creates a local value called total and the calling code needs the calculated value. What should the subprogram do?
  1. Return the value so the calling code can receive it
  2. Assume total is automatically available everywhere
  3. Rename the parameter to total
  4. Remove the calculation
Show answer and explanation
Return the value so the calling code can receive it
A return statement sends the result to the caller. The local name total does not itself make the value available outside the subprogram.

Question 3

For def double(value): return value * 2, what value does double(6) return?
  1. 3
  2. 6
  3. 8
  4. 12
Show answer and explanation
12
The call supplies 6 for value, and the subprogram returns twice that value, which is 12.

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

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.

Official curriculum reference

Report a correction or ask a question