Local variables

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

Local variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates a local variable inside a subroutine?
A LOCAL Length INTEGER
B DECLARE Length : INTEGER
2. A variable is declared inside a procedure. Where can it be used?
A Only inside that procedure
B Anywhere in the whole program
3. PROCEDURE ShowArea() DECLARE Area : INTEGER Area ← 20 ENDPROCEDURE CALL ShowArea() OUTPUT Area What is wrong with the main program?
A Area should have been declared after ENDPROCEDURE
B Area is local to ShowArea, so the main program cannot use it
4. When does a local variable exist?
A Only while the subroutine is running
B For the whole time the program is running
5. Is a parameter in a subroutine local or global?
A Local to the subroutine
B Global to the whole program

Match the Pairs

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

Left columnRight column
1DECLARE Total : INTEGER AFUNCTION Double(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result <- N * 2 RETURN Result ENDFUNCTION
2PROCEDURE ShowArea(Width : INTEGER) BCount is declared outside the procedure in the main program, so it is not a local variable of Greet. DECLARE must be inside the procedure body.
3A local variable is declared inside the body of a function, before it is used CWidth is a parameter, which is also a local variable that can only be used inside ShowArea
4A local variable is not available in the main program after the subroutine has finished DCreates a local variable called Total inside a subroutine, which can only be used in that subroutine
5PROCEDURE Greet() OUTPUT Count ENDPROCEDURE DECLARE Count : INTEGER ECALL ShowArea(5) OUTPUT Length
6CALL Calculate() OUTPUT Length FLength is a local variable declared inside Calculate, so the main program cannot use it after the call.

Definitions

DECLARE

Fill in the Blanks

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

1.
PROCEDURE ShowArea() _______ Length : INTEGER ENDPROCEDURE
2.
PROCEDURE ShowPrice() DECLARE Price : ____ Price ← 2.5
3.
PROCEDURE ShowLength() DECLARE Length : INTEGER Length _ 5
4.
PROCEDURE Show(Width : INTEGER) OUTPUT _____ * 2 ENDPROCEDURE
5.
PROCEDURE Show() DECLARE ____ : INTEGER Mark ← 80

Spot the Error

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

Question 1
A PROCEDURE ShowArea()
B DECLARE Area INTEGER
C Area ← 5 * 4
D OUTPUT Area
E ENDPROCEDURE
Question 2
A PROCEDURE SetUp()
B DIM Count : INTEGER
C Count ← 0
D ENDPROCEDURE
Question 3
A PROCEDURE Hello()
B DECLARE Msg : STRING
C Msg = "Hi"
D OUTPUT Msg
E ENDPROCEDURE
Question 4
A PROCEDURE Show(N)
B DECLARE Temp : INTEGER
C Temp ← N * 2
D OUTPUT Temp
E ENDPROCEDURE
Question 5
A FUNCTION Age() RETURNS INTEGER
B DECLARE Years : INTEGER
C Yrs ← 15
D RETURN Years
E ENDFUNCTION

Answer Key

Local variables
Section A: Quick Questions
Q1
B — DECLARE Length : INTEGER
Q2
A — Only inside that procedure
Q3
B — Area is local to ShowArea, so the main program cannot use it
Q4
A — Only while the subroutine is running
Q5
A — Local to the subroutine
Section B: Match the Pairs
Q1
DECLARE Total : INTEGER → Creates a local variable called Total inside a subroutine, which can only be used in that subroutine
Q2
PROCEDURE ShowArea(Width : INTEGER) → Width is a parameter, which is also a local variable that can only be used inside ShowArea
Q3
A local variable is declared inside the body of a function, before it is used → FUNCTION Double(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result <- N * 2 RETURN Result ENDFUNCTION
Q4
A local variable is not available in the main program after the subroutine has finished → CALL ShowArea(5) OUTPUT Length
Q5
PROCEDURE Greet() OUTPUT Count ENDPROCEDURE DECLARE Count : INTEGER → Count is declared outside the procedure in the main program, so it is not a local variable of Greet. DECLARE must be inside the procedure body.
Q6
CALL Calculate() OUTPUT Length → Length is a local variable declared inside Calculate, so the main program cannot use it after the call.
Section C: Definitions
Q1
DECLARE — Creates a local variable inside a procedure or function, e.g. DECLARE Length : INTEGER. It exists only while the subroutine runs and cannot be used in the main program.
Section D: Fill in the Blanks
Q1
PROCEDURE ShowArea() DECLARE Length : INTEGER ENDPROCEDURE
Q2
PROCEDURE ShowPrice() DECLARE Price : REAL Price ← 2.5
Q3
PROCEDURE ShowLength() DECLARE Length : INTEGER Length ← 5
Q4
PROCEDURE Show(Width : INTEGER) OUTPUT Width * 2 ENDPROCEDURE
Q5
PROCEDURE Show() DECLARE Mark : INTEGER Mark ← 80
Section E: Spot the Error
Q1
B — A local variable is declared with DECLARE, a name, a colon and a data type. The colon is missing. It should be DECLARE Area : INTEGER.
Q2
B — DIM is not the pseudocode keyword for declaring a local variable. Inside a procedure it must be DECLARE Count : INTEGER.
Q3
C — Assignment in IGCSE pseudocode uses the arrow ←, not =. The line should be Msg ← "Hi".
Q4
A — A parameter is a local variable too, so it needs a data type in the header. It should be PROCEDURE Show(N : INTEGER).
Q5
C — Yrs was never declared in this function, so it does not exist as a local variable. The line should use the declared name: Years ← 15.