Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.DECLARE Lives : INTEGER
PROCEDURE LoseLife()
Lives ← Lives - 1PROCEDURE Greet()
DECLARE Count : INTEGER
ENDPROCEDURE// Global variable: Score
PROCEDURE Reset()
DECLARE Score : INTEGER // same namePROCEDURE Count()
DECLARE Total : INTEGER
ENDPROCEDURE // Total stops existing hereDECLARE Limit : INTEGER
FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN
RETURN N > Limit