17-scope-parameters-arrays.py
python-programming / subprograms  ·  Lesson 18
Lesson 18 — Subprograms

Scope, Parameters & Arrays

Use local and global values carefully, and pass arrays into and out of subprograms.

Variables created inside a subprogram are local to it. Variables created outside are global. Passing data through parameters and return values usually makes subprograms clearer and easier to reuse.

Predict

Read the code first. Do not run it yet.

scope-parameters-arrays-predict.py
RATE = 2

def calculate(value):
    result = value * RATE
    return result

print(calculate(5))
Predict before running
  • Which name is a global constant?
  • Which names are local to the function?
  • What value is returned?

Investigate

Use this code to explain exactly how the program works.

scope-parameters-arrays-investigate.py
def add_one(values):
    result = [0] * len(values)
    for i in range(len(values)):
        result[i] = values[i] + 1
    return result

numbers = [4, 7, 9]
changed = add_one(numbers)
print(changed)
Investigate the code
  • Which array is passed into the function?
  • Which local array is created?
  • Which array is returned?

Local and global scope

scope.py
school = "Central"

def show_class():
    room = "B12"
    print(school, room)

show_class()
Output
Central B12
How it works
  • school is defined outside the subprogram, so it is global.
  • room is created inside the subprogram, so it is local.
  • Local variables exist for the work of that subprogram and are not used directly outside it.

Local constants

local_constant.py
def circle_area(radius):
    PI = 3.142
    return PI * radius * radius

print(circle_area(2))
Output
12.568
How it works
  • PI is created inside the function, so it is local.
  • Its uppercase name shows that the program intends to keep this value unchanged.

Passing and returning arrays

arrays.py
def square_values(values):
    result = [0] * len(values)
    for i in range(len(values)):
        result[i] = values[i] * values[i]
    return result

print(square_values([2, 3, 4]))
Output
[4, 9, 16]
Prefer clear data flowUse parameters for inputs and return values for outputs where practical. Global values are sometimes useful, especially constants, but too many global variables can make a program harder to follow.

Self-study guide

Scope describes where a name can be accessed. A local variable or parameter exists within a subprogram call; a global value is defined outside subprograms and can be read more widely. OCR also expects students to use local/global variables and constants and to pass and return arrays.

Good structure usually prefers parameters, return values and local working variables over unnecessary global variables. Global constants can be useful for genuinely fixed rules shared by several subprograms.

OCR focusUnderstand local/global scope, parameters, constants, and passing/returning arrays in functions and procedures. Do not assume OCR global declaration maps directly to writing global everywhere in Python.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
global RATE = 3RATE = 3 outside functionsA Python name defined at module level is global.
parameterfunction parameterLocal name receiving an argument.
local variablevariable assigned inside functionNormally accessible only inside that function call.
array passed as parameterlist passed as argumentPython passes the list object to the subprogram.
return arrayreturn listThe caller can store the returned list.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This example passes an array into a function and returns a separate fixed-length result array.

lesson-18-worked.py
BOOST = 3

def boost_scores(values):
    result = [0, 0, 0, 0]

    for index in range(len(values)):
        result[index] = values[index] + BOOST

    return result

scores = [5, 8, 2, 6]
boosted = boost_scores(scores)

print(scores)
print(boosted)
Output
[5, 8, 2, 6]
[8, 11, 5, 9]
Work through it line by line
  • BOOST is a global constant by convention.
  • values, result and index are local to the function call.
  • The original array is read but a separate result array is filled.
  • Returning result allows the main program to receive the new array.

Another example

A local variable with the same idea as a global does not automatically replace the global value elsewhere.

lesson-18-example-2.py
RATE = 2

def calculate(points):
    bonus = points * RATE
    return bonus

print(calculate(5))
Output
10
Why this example matters
  • RATE is read from global scope.
  • bonus exists only inside calculate() while it runs.

Common mistakes

Avoid these errors
  • Using globals for every value instead of parameters and returns.
  • Writing Python global unnecessarily just because OCR ERL has a global declaration.
  • Assuming a local variable can be accessed from the main program after the function ends.
  • Changing the size of an OCR-style array when the task expects fixed/static structure.
Exam-safe warningIn Python, the global statement is only required when code inside a function assigns to a global name. Simply reading a global constant does not require it.

Exam-ready checklist

You should now be able to
  • Identify local and global names.
  • Explain why local scope can improve maintainability.
  • Pass arrays into subprograms and return arrays from functions.
  • Use global constants sensibly.
  • Translate OCR scope ideas accurately into Python.

What you have learned

  • Local variables and local constants belong to a subprogram.
  • Global variables or constants are defined outside subprograms.
  • Arrays can be passed as parameters.
  • Arrays can also be returned from functions.