Local compared with global variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Local compared with global variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A variable declared inside a subroutine is called...
A A local variable
B A global variable
2. Which variable can be used anywhere in the program?
A A local variable
B A global variable
3. When does a local variable exist?
A Only while its subroutine is running
B For the whole time the program runs
4. The main program tries to OUTPUT a variable that was declared inside a procedure. What happens?
A An error, because the local variable is out of scope
B It outputs the value the procedure gave it
5. Where is a global variable declared?
A Inside one subroutine
B Outside all subroutines

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1DECLARE Lives : INTEGER // outside all subroutines ATemp is a local variable of Calc, so it does not exist outside the procedure. Using it in the main program causes an error. Fix: OUTPUT Temp inside Calc, or use a function to RETURN the value.
2DECLARE Temp : INTEGER // inside PROCEDURE Show() BDECLARE Count : INTEGER PROCEDURE Reset() DECLARE Count : INTEGER Count ← 0 ENDPROCEDURE
3A local variable with the same name as a global variable is a separate variable, so the subroutine changes its own copy and the global keeps its value. CA local variable: it only exists and can only be used inside Show while Show is running.
4A global variable can be used inside any subroutine without being passed in as a parameter. DA global variable: it can be used anywhere in the program and exists for the whole time the program runs.
5PROCEDURE Calc() DECLARE Temp : INTEGER Temp ← 5 ENDPROCEDURE CALL Calc() OUTPUT Temp EDECLARE Total : INTEGER PROCEDURE ShowTotal() OUTPUT Total ENDPROCEDURE
6PROCEDURE A() DECLARE X : INTEGER X ← 10 ENDPROCEDURE PROCEDURE B() OUTPUT X ENDPROCEDURE FX is local to procedure A, so procedure B cannot use it. Fix: declare X as a global variable, or pass it to B as a parameter.

Definitions

local vs global

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
DECLARE Lives : INTEGER PROCEDURE LoseLife() Lives ← _____ - 1
2.
PROCEDURE Greet() _______ Count : INTEGER ENDPROCEDURE
3.
// Global variable: Score PROCEDURE Reset() DECLARE _____ : INTEGER // same name
4.
PROCEDURE Count() DECLARE Total : INTEGER ____________ // Total stops existing here
5.
DECLARE Limit : INTEGER FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN _ > Limit

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A PROCEDURE Show(Num : INTEGER)
B OUTPUT Num
C ENDPROCEDURE
D CALL Show(Num)
Question 2
A DECLARE Lives : INTEGER
B PROCEDURE Reset()
C DECLARE Lives : INTEGER
D ENDPROCEDURE
E CALL Reset()
F OUTPUT Lives
Question 3
A DECLARE Total : INTEGER
B CALL Add(10)
C OUTPUT Num
D PROCEDURE Add(Num : INTEGER)
E Total ← Num
F ENDPROCEDURE
Question 4
A PROCEDURE Double()
B OUTPUT Num * 2
C ENDPROCEDURE
D CALL Double(4)
Question 5
A DECLARE Pts : INTEGER
B PROCEDURE Add(Pts : INTEGER)
C Pts ← Pts + 5
D ENDPROCEDURE
E CALL Add()
F OUTPUT Pts

Answer Key

Local compared with global variables
Section A: Quick Questions
Q1
A — A local variable
Q2
B — A global variable
Q3
A — Only while its subroutine is running
Q4
A — An error, because the local variable is out of scope
Q5
B — Outside all subroutines
Section B: Match the Pairs
Q1
DECLARE Lives : INTEGER // outside all subroutines → A global variable: it can be used anywhere in the program and exists for the whole time the program runs.
Q2
DECLARE Temp : INTEGER // inside PROCEDURE Show() → A local variable: it only exists and can only be used inside Show while Show is running.
Q3
A local variable with the same name as a global variable is a separate variable, so the subroutine changes its own copy and the global keeps its value. → DECLARE Count : INTEGER PROCEDURE Reset() DECLARE Count : INTEGER Count ← 0 ENDPROCEDURE
Q4
A global variable can be used inside any subroutine without being passed in as a parameter. → DECLARE Total : INTEGER PROCEDURE ShowTotal() OUTPUT Total ENDPROCEDURE
Q5
PROCEDURE Calc() DECLARE Temp : INTEGER Temp ← 5 ENDPROCEDURE CALL Calc() OUTPUT Temp → Temp is a local variable of Calc, so it does not exist outside the procedure. Using it in the main program causes an error. Fix: OUTPUT Temp inside Calc, or use a function to RETURN the value.
Q6
PROCEDURE A() DECLARE X : INTEGER X ← 10 ENDPROCEDURE PROCEDURE B() OUTPUT X ENDPROCEDURE → X is local to procedure A, so procedure B cannot use it. Fix: declare X as a global variable, or pass it to B as a parameter.
Section C: Definitions
Q1
local vs global — A local variable is declared inside a subroutine and only exists and works there while it runs. A global variable is declared outside and can be used anywhere in the program all the time.
Section D: Fill in the Blanks
Q1
DECLARE Lives : INTEGER PROCEDURE LoseLife() Lives ← Lives - 1
Q2
PROCEDURE Greet() DECLARE Count : INTEGER ENDPROCEDURE
Q3
// Global variable: Score PROCEDURE Reset() DECLARE Score : INTEGER // same name
Q4
PROCEDURE Count() DECLARE Total : INTEGER ENDPROCEDURE // Total stops existing here
Q5
DECLARE Limit : INTEGER FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N > Limit
Section E: Spot the Error
Q1
D — Num is a parameter, which is a local variable that only exists inside Show. The main program cannot use it. The call must pass a real value, such as CALL Show(7).
Q2
C — Declaring Lives inside Reset creates a new local variable with the same name, which hides the global one. The global Lives is never set. Remove the DECLARE and assign Lives ← 3 so the global is changed.
Q3
C — Num is a local parameter of Add and does not exist outside it, so the main program cannot output it. It should output the global variable Total, which can be used anywhere.
Q4
A — The parameter Num is a local variable that is created by the procedure header. Without Num in the header, the line OUTPUT Num * 2 uses a variable that is not in scope. The header should be PROCEDURE Double(Num : INTEGER).
Q5
B — A parameter is a local variable. Naming it Pts hides the global Pts, so the procedure would change only the local copy and the global stays the same. The header should be PROCEDURE Add() so the global Pts is used.