Why local variables are preferred

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

Why local variables are preferred

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why are local variables usually preferred over global variables?
A They stop other parts of the program changing the value by mistake
B They make the program run faster on every computer
2. Two procedures both declare a local variable called Total. What happens?
A They share one Total, so changing it in one procedure changes it in the other
B Each procedure has its own separate Total, so they do not affect each other
3. Which is the better way to give a subroutine a value it needs?
A Set a global variable before calling the subroutine
B Pass the value in as a parameter
4. A procedure changes the global variable Score by mistake. What is the problem?
A Other parts of the program that use Score may now get wrong values
B Nothing, because only that procedure can see Score
5. Which subroutine is easier to test on its own?
A One that uses only its parameters and local variables
B One that reads and changes several global variables

Match the Pairs

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

Left columnRight column
1DECLARE Total : INTEGER (inside PROCEDURE ShowTotal) AA local variable: it exists only inside this subroutine, so no other part of the program can change it by mistake
2FUNCTION AddTax(Price : REAL) RETURNS REAL BOUTPUT Double(5) // should output 10
3Two subroutines can each use a local variable with the same name without affecting each other CThe procedure changes the global variable Score by mistake, which can break other parts of the program. Use a local variable instead.
4A subroutine that uses only local variables and parameters can be tested on its own DThe value is passed in as a parameter, so the function does not rely on a global variable and can be tested alone
5Score ← 0 (global) PROCEDURE Reset() Score ← 100 ENDPROCEDURE EThe function depends on global variables Width and Height, so it is hard to test alone. Pass them in as parameters instead.
6FUNCTION Area() RETURNS INTEGER RETURN Width * Height ENDFUNCTION FPROCEDURE A() DECLARE i : INTEGER ... PROCEDURE B() DECLARE i : INTEGER

Definitions

local variable

Fill in the Blanks

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

1.
PROCEDURE AddBonus(Score : INTEGER) _______ NewScore : INTEGER NewScore ← Score + 10
2.
FUNCTION AddTax(Price : REAL) RETURNS REAL DECLARE Total : ____ Total ← Price * 1.2
3.
PROCEDURE Reset() DECLARE Count : INTEGER _____ ← 0
4.
FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN _ * N ENDFUNCTION
5.
// Test Square alone: expect 16 ______ Square(4)

Spot the Error

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

Question 1
A PROCEDURE Twice()
B OUTPUT N * 2
C ENDPROCEDURE
Question 2
A PROCEDURE Bonus(P : INTEGER)
B OUTPUT Pts + 10
C ENDPROCEDURE
Question 3
A PROCEDURE Calc(Num : INTEGER)
B DECLARE Temp : INTEGER
C Total ← Num * 2
D OUTPUT Temp
E ENDPROCEDURE
Question 4
A PROCEDURE Add(N : INTEGER)
B DECLARE Sum : INTEGER
C Sum ← Total + 1
D OUTPUT Sum
E ENDPROCEDURE
Question 5
A FUNCTION Inc(N) RETURNS REAL
B DECLARE R : REAL
C R ← N + 1
D RETURN Total
E ENDFUNCTION

Answer Key

Why local variables are preferred
Section A: Quick Questions
Q1
A — They stop other parts of the program changing the value by mistake
Q2
B — Each procedure has its own separate Total, so they do not affect each other
Q3
B — Pass the value in as a parameter
Q4
A — Other parts of the program that use Score may now get wrong values
Q5
A — One that uses only its parameters and local variables
Section B: Match the Pairs
Q1
DECLARE Total : INTEGER (inside PROCEDURE ShowTotal) → A local variable: it exists only inside this subroutine, so no other part of the program can change it by mistake
Q2
FUNCTION AddTax(Price : REAL) RETURNS REAL → The value is passed in as a parameter, so the function does not rely on a global variable and can be tested alone
Q3
Two subroutines can each use a local variable with the same name without affecting each other → PROCEDURE A() DECLARE i : INTEGER ... PROCEDURE B() DECLARE i : INTEGER
Q4
A subroutine that uses only local variables and parameters can be tested on its own → OUTPUT Double(5) // should output 10
Q5
Score ← 0 (global) PROCEDURE Reset() Score ← 100 ENDPROCEDURE → The procedure changes the global variable Score by mistake, which can break other parts of the program. Use a local variable instead.
Q6
FUNCTION Area() RETURNS INTEGER RETURN Width * Height ENDFUNCTION → The function depends on global variables Width and Height, so it is hard to test alone. Pass them in as parameters instead.
Section C: Definitions
Q1
local variable — A variable declared inside a subroutine. It keeps the subroutine self-contained, so changing it cannot accidentally alter other parts of the program, and the subroutine is easier to test alone.
Section D: Fill in the Blanks
Q1
PROCEDURE AddBonus(Score : INTEGER) DECLARE NewScore : INTEGER NewScore ← Score + 10
Q2
FUNCTION AddTax(Price : REAL) RETURNS REAL DECLARE Total : REAL Total ← Price * 1.2
Q3
PROCEDURE Reset() DECLARE Count : INTEGER Count ← 0
Q4
FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q5
// Test Square alone: expect 16 OUTPUT Square(4)
Section E: Spot the Error
Q1
A — The header has no parameter, so N is never passed in. The procedure would have to rely on a global variable called N. It should be PROCEDURE Twice(N : INTEGER) so the value is passed in as a parameter.
Q2
A — The parameter is called P, but the body uses Pts. Pts is not local to the procedure, so it would only work if a global called Pts existed. The parameter name must match the name used inside: Pts.
Q3
C — Total is not declared in this procedure, so this line would change a global variable by mistake. The procedure should store the result in its own local variable: Temp ← Num * 2.
Q4
C — Total is not a parameter or a local variable, so the procedure depends on a global variable outside it. That makes it hard to test alone. It should use the parameter: Sum ← N + 1.
Q5
D — The function works out its answer in the local variable R, but returns Total, which is some other variable outside the function. It should be RETURN R.