Global variables

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

Global variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where is a global variable declared?
A Inside one procedure only
B Outside every subroutine, at the start of the program
2. Which line declares a global variable in IGCSE pseudocode?
A DECLARE Score : INTEGER (before any PROCEDURE)
B GLOBAL Score = 0 inside a FUNCTION
3. Which parts of a program can use a global variable?
A Only the main program
B The main program and every procedure and function
4. A global variable Total is declared at the start. A procedure runs Total ← Total + 5. What happens to the global Total?
A Its value changes for the whole program
B Only a temporary copy changes inside the procedure
5. How long does a global variable exist?
A For the whole time the program runs
B Only while one subroutine is running

Match the Pairs

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

Left columnRight column
1DECLARE Score : INTEGER AScore is declared after the procedure, not at the start of the program. The global DECLARE should come first, before the subroutines and the main program use it.
2Score ← Score + 10 BFUNCTION DoubleScore() RETURNS INTEGER RETURN Score * 2 ENDFUNCTION
3A global variable is available everywhere, so a function can use it without it being passed in as a parameter CCALL AddPoints() OUTPUT Score
4A global variable exists for the whole program, so the main program can output it after a procedure has changed it DLives is declared inside the procedure, so it is a local variable. The main program cannot use it. To make it global, declare it at the start of the program, outside the procedure.
5PROCEDURE SetUp() DECLARE Lives : INTEGER Lives ← 3 ENDPROCEDURE OUTPUT Lives EWhen written at the start of the program, outside every subroutine, this creates a global variable that the whole program can use
6PROCEDURE AddPoints() Score ← Score + 5 ENDPROCEDURE DECLARE Score : INTEGER FInside a procedure, this changes the global variable Score, and the new value is still there after the procedure ends

Definitions

global variable

Fill in the Blanks

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

1.
_______ Score : INTEGER
2.
DECLARE Score : _______ Score ← 0
3.
PROCEDURE AddPoint() _____ ← Score + 1 ENDPROCEDURE
4.
FUNCTION DoubleScore() RETURNS INTEGER RETURN _____ * 2 ENDFUNCTION
5.
DECLARE Score : INTEGER Score ← 0 ____ AddPoint()

Spot the Error

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

Question 1
A DECLARE Score : INTEGER
B Score = 0
C PROCEDURE AddPoint()
D Score ← Score + 1
E ENDPROCEDURE
Question 2
A DECLARE Total : INTEGER
B Total ← 0
C FUNCTION AddTen()
D Total ← Total + 10
E ENDPROCEDURE
Question 3
A DECLARE Coins : INTEGER
B PROCEDURE Spend()
C Coin ← Coins - 1
D ENDPROCEDURE
E Coins ← 5
F CALL Spend()
Question 4
A DECLARE Name = STRING
B Name ← "Sam"
C PROCEDURE Greet()
D OUTPUT "Hi ", Name
E ENDPROCEDURE
Question 5
A DECLARE Level : INTEGER
B Level ← "1"
C PROCEDURE NextLevel()
D Level ← Level + 1
E ENDPROCEDURE

Answer Key

Global variables
Section A: Quick Questions
Q1
B — Outside every subroutine, at the start of the program
Q2
A — DECLARE Score : INTEGER (before any PROCEDURE)
Q3
B — The main program and every procedure and function
Q4
A — Its value changes for the whole program
Q5
A — For the whole time the program runs
Section B: Match the Pairs
Q1
DECLARE Score : INTEGER → When written at the start of the program, outside every subroutine, this creates a global variable that the whole program can use
Q2
Score ← Score + 10 → Inside a procedure, this changes the global variable Score, and the new value is still there after the procedure ends
Q3
A global variable is available everywhere, so a function can use it without it being passed in as a parameter → FUNCTION DoubleScore() RETURNS INTEGER RETURN Score * 2 ENDFUNCTION
Q4
A global variable exists for the whole program, so the main program can output it after a procedure has changed it → CALL AddPoints() OUTPUT Score
Q5
PROCEDURE SetUp() DECLARE Lives : INTEGER Lives ← 3 ENDPROCEDURE OUTPUT Lives → Lives is declared inside the procedure, so it is a local variable. The main program cannot use it. To make it global, declare it at the start of the program, outside the procedure.
Q6
PROCEDURE AddPoints() Score ← Score + 5 ENDPROCEDURE DECLARE Score : INTEGER → Score is declared after the procedure, not at the start of the program. The global DECLARE should come first, before the subroutines and the main program use it.
Section C: Definitions
Q1
global variable — A variable declared outside every subroutine, so the main program and any procedure or function can use and change it. It exists for the whole program.
Section D: Fill in the Blanks
Q1
DECLARE Score : INTEGER
Q2
DECLARE Score : INTEGER Score ← 0
Q3
PROCEDURE AddPoint() Score ← Score + 1 ENDPROCEDURE
Q4
FUNCTION DoubleScore() RETURNS INTEGER RETURN Score * 2 ENDFUNCTION
Q5
DECLARE Score : INTEGER Score ← 0 CALL AddPoint()
Section E: Spot the Error
Q1
B — Assignment in IGCSE pseudocode uses the ← arrow, not =. The global variable Score should be set with Score ← 0.
Q2
C — AddTen changes the global variable Total but returns nothing, so it is a procedure. The line should be PROCEDURE AddTen() to match ENDPROCEDURE.
Q3
C — The global variable is called Coins, not Coin. The procedure must use the exact declared name: Coins ← Coins - 1.
Q4
A — A DECLARE statement gives the data type after a colon. It should be DECLARE Name : STRING.
Q5
B — Level was declared as an INTEGER, so it cannot store the string "1". It should be Level ← 1.