Pearson Edexcel GCSE Computer Science (1CP2)Specification point 6.6.3

Edexcel 1CP2 6.6.3: Local and global variables

The free BugBot lesson that teach this point, a robot to try it on, and the past-paper questions that test it.

What it covers

A variable made inside a function is not the same as one made outside it, even when they have the same name. Where a variable can be used is called its scope. Getting scope wrong gives some of the most confusing errors in Python; getting it right is what lets functions be written and tested one at a time.

  • Local variables stay inside
  • Same name, different variables
  • Reading a global works; changing it does not
  • Fix 1: pass it in and return it
  • Fix 2: the global keyword
Read the free lesson (15 min)

Or do it in the simulator, with tasks that mark themselves

Lessons that teach it

  1. F4.3 Local and global variables Functions and structured code
    Scope: where a variable exists, and why passing values beats global.

Try it on a robot

The task from F4.3 Local and global variables, running here. Change the program and press Run.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.

The specification is Edexcel's own document; this page says which of our lessons teach point 6.6.3 and is not endorsed by the board.