Where Variables Live

Test yourself on Variable Scope with exam style questions, marked instantly.

Flashcards that come back just before you would forget them.

A revision plan that remembers what you know and shows what to do next.

See which topics you have nailed and which need another look.

Quiz yourself on Variable ScopeGet your flashcardsBuild my revision planTrack my progress

Where Variables Live

Scope is where a variable can be seen and used. A local variable is like your bedroom - only you can access it, and when you leave, it's cleaned up. A global variable is like the living room - everyone in the house can use it, and it's always there. Local variables exist only inside their function. Global variables exist everywhere. Generally, prefer local - it's tidier and safer!

Nested boxes. The large outer box, Global scope, holds totalScore = 0 and playerName = "Alice". Inside it are two function boxes, each a Local scope. addPoints() holds bonus = 10 and prints totalScore + bonus. greetPlayer() holds message = "Hello" and prints message + playerName. Both functions can read the global variables. bonus exists only in addPoints() and message only in greetPlayer().

Each function box is its own local scope, inside the global scope. Global variables are named by OCR and Edexcel. In OCR pseudocode a global variable is declared with the word global, for example global userID = "Cust001". AQA names local variables and why they are good practice.

Make this stick

Signing in gives access to all PrepWise features.

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.