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.
RATE = 2
def calculate(value):
result = value * RATE
return result
print(calculate(5))- 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.
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)- Which array is passed into the function?
- Which local array is created?
- Which array is returned?
Local and global scope
school = "Central"
def show_class():
room = "B12"
print(school, room)
show_class()Central B12
schoolis defined outside the subprogram, so it is global.roomis 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
def circle_area(radius):
PI = 3.142
return PI * radius * radius
print(circle_area(2))12.568
PIis 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
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]))[4, 9, 16]
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.
global declaration maps directly to writing global everywhere in Python.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
global RATE = 3 | RATE = 3 outside functions | A Python name defined at module level is global. |
parameter | function parameter | Local name receiving an argument. |
local variable | variable assigned inside function | Normally accessible only inside that function call. |
array passed as parameter | list passed as argument | Python passes the list object to the subprogram. |
return array | return list | The caller can store the returned list. |
Worked example
This example passes an array into a function and returns a separate fixed-length result array.
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)[5, 8, 2, 6] [8, 11, 5, 9]
BOOSTis a global constant by convention.values,resultandindexare local to the function call.- The original array is read but a separate result array is filled.
- Returning
resultallows 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.
RATE = 2
def calculate(points):
bonus = points * RATE
return bonus
print(calculate(5))10
RATEis read from global scope.bonusexists only insidecalculate()while it runs.
Common mistakes
- Using globals for every value instead of parameters and returns.
- Writing Python
globalunnecessarily just because OCR ERL has aglobaldeclaration. - 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.
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
- 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.