Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Subprograms

Scope + parameters

All topicsPractise exam questions
Knowledge organiser

Subprograms

301.12d

Local vs global.

What you need to know

Variables defined inside a function are local — they only exist inside that function. Parameters pass data into a function. Global variables exist outside all functions.

Key points

  • Local variable: created inside a function, dies when function ends
  • Global variable: created outside, accessible everywhere
  • Parameters: values passed into a function when called
  • Avoid using global variables — pass data via parameters instead

Code examples

Scope example
python
def double(n):      # n is a parameter (local)
    result = n * 2  # result is local
    return result

x = 5               # x is global
print(double(x))    # 10
# print(result)     # Error — result doesn't exist here