Procedures, Functions, Arrays and File Handling: Question 1
Syllabus 8.1
A program uses a global variable, TicketsSold, and a procedure that declares its own local variable with the identical identifier, to record how many cinema tickets have been sold.
DECLARE TicketsSold : INTEGER
TicketsSold ← 10
PROCEDURE ResetCounter()
DECLARE TicketsSold : INTEGER
TicketsSold ← 0
OUTPUT TicketsSold
ENDPROCEDURE
CALL ResetCounter()
OUTPUT TicketsSold
What is displayed, in order, by the two OUTPUT statements above?
Show worked solution Hide worked solution
Worked solution
Step 1: Identify the two variables that share the identifier TicketsSold
The global TicketsSold is declared before any procedure, and its value is set to 10. The
procedure ResetCounter then contains its own line DECLARE TicketsSold : INTEGER. This
statement creates a brand-new, local variable that happens to have the same identifier as the
global one, but it is a completely separate piece of storage that exists only while the
procedure is running.
Step 2: Trace what happens inside the procedure
CALL ResetCounter() runs the procedure body:
DECLARE TicketsSold : INTEGERcreates the local variable.TicketsSold ← 0sets this local variable to 0.OUTPUT TicketsSolddisplays the local variable’s value: 0.
The global TicketsSold, still holding 10, is never touched by any of these three lines.
Step 3: Trace what happens after the procedure ends
Once ResetCounter finishes running, its local TicketsSold is destroyed. Control returns to
the main program, where the identifier TicketsSold now refers to the global variable again.
Since the procedure only ever changed its own local copy, the global variable is unaffected and
still holds 10, so OUTPUT TicketsSold displays 10.
Final answer
- First OUTPUT (inside the procedure): 0
- Second OUTPUT (after the procedure call finishes): 10
- Displayed, in order: 0 then 10, option B