Computer ScienceExam code: 0984

Procedures and Functions

Programming

The of a variable is where in the program that variable can be seen and used.

A variable's scope depends on where it is declared. Two common scopes:

  • Local scope: the variable is visible only inside a particular sub-program or block.
  • Global scope: the variable is visible everywhere in the program.

Local variables

A local variable is one that is declared inside a (procedure or function). It only exists while that sub-program is running; once the sub-program returns, the local variable is destroyed.

Example:

PROCEDURE ShowDouble(Number : INTEGER)
   DECLARE Doubled : INTEGER
   Doubled ← Number * 2
   OUTPUT Doubled
ENDPROCEDURE

CALL ShowDouble(7)
OUTPUT Doubled       // ERROR: Doubled does not exist here

Doubled is local to ShowDouble. The main program cannot see it. As soon as the procedure ends, Doubled is forgotten.

Global variables

A global variable is one that is declared outside any sub-program, at the top level of the program. It is visible throughout the entire program, including inside sub-programs.

Example:

PROCEDURE AddPoint
   Score ← Score + 1       // can see the global Score
ENDPROCEDURE

DECLARE Score : INTEGER
Score ← 0
CALL AddPoint
CALL AddPoint
OUTPUT Score                // outputs 2

Score is declared at the top level, so any sub-program can read and update it.

Why prefer local variables when possible

Although global variables are convenient, using them too freely makes programs hard to maintain:

  • A bug that changes a global value can affect parts of the program far away from where the bug is.
  • Two sub-programs that happen to use the same variable name globally will accidentally interfere with each other.
  • Reading a program with many globals requires holding the whole program in your head at once.

Best practice: use by default, and use and return values to pass information between sub-programs. Reserve globals for genuinely shared state, such as a running total or a configuration setting.

Side-by-side comparison

FeatureLocal variableGlobal variable
Where declaredInside a sub-program (procedure or function)Outside any sub-program (top level)
Visible fromOnly inside the sub-program that declared itAnywhere in the program
LifetimeCreated when the sub-program starts; destroyed when it endsLives for the whole program run
Risk of accidental interferenceLow: name clashes are local to the blockHigher: any code can change the value
Best forTemporary calculations, loop counters, intermediate resultsShared state, configuration, totals across many sub-programs
Preferred styleDefault choiceUse sparingly

Common exam question

Matching sub-program terms to their descriptions

Question: Draw lines matching descriptions to procedure, function, parameter, local variable and global variable, or tick the term for passing a value to a procedure (1–4 marks).

Asked in 2 of the 17 papers, one mark per correct match. A sub-program that might not return a value is a procedure; one that always returns a value is a function. A value a procedure must be given when it is called is a parameter, which is also how a value is passed in; return sends a function's result back, and subroutine names either kind. A variable declared and used inside one procedure only is local; a global variable is declared outside every sub-program and is usable anywhere.

The matching version has one more term than descriptions, so place the certain ones first. The papers and schemes usually call the values passed in a call parameters too, so expect that word where these notes say argument.

Spotting scope issues

Most scope mistakes show up in three patterns. Knowing the symptoms makes them easy to fix:

  • A name appears to vanish. A variable assigned inside a procedure cannot be read by the main program afterwards. If the main program tries, it causes an error, because the variable does not exist outside the procedure. Fix by returning the value (use a function) or by writing to a genuinely shared global.
  • Two variables share a name by accident. A procedure declares a local variable whose name matches a global. Inside the procedure, writes go to the local one; the global is left untouched. The bug looks like "the total never updates". Fix by renaming one of them, or by passing the value in as a parameter.
  • A counter resets on every call. A loop variable declared inside a procedure is destroyed when the procedure ends. Calling the procedure again starts the counter from scratch. If the intent was a running total across calls, the variable belongs at the top level as a global.

A quick rule of thumb: if a value is needed in only one block, declare it locally; if several sub-programs must share it, pass it as a parameter or return it. Reach for a global only when the value really is shared state for the whole program.

Quick check

5 short questions on this note. See what has stuck, and where to look if something hasn't.

About 2 minutes

Build on this topic