Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Finding and fixing errors in subroutine code worksheet is part of the IGCSE Computer Science 0478 — 8.1c Procedures and Functions journey. Print it as a booklet?
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 | |
|---|---|---|
| 1FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION | AFUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION | |
| 2CALL ShowTotal(5, 10) // header: PROCEDURE ShowTotal(A : INTEGER) | BFUNCTION AddTwo(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result ← N + 2 RETURN Result ENDFUNCTION | |
| 3Every FUNCTION must be closed with ENDFUNCTION, and every PROCEDURE with ENDPROCEDURE | CThe ENDFUNCTION keyword is missing, so the function is never closed. Add ENDFUNCTION on the last line. | |
| 4A local variable only exists inside the subroutine that declares it, so the value must be returned or passed out | DThis function has no RETURN statement, so it never sends a value back to the caller. Add RETURN N * 2. | |
| 5FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18 | ESum is a local variable declared inside the procedure, so it cannot be used in OUTPUT outside it. Output it inside the procedure. | |
| 6PROCEDURE Total() DECLARE Sum : INTEGER Sum ← 5 + 3 ENDPROCEDURE OUTPUT Sum | FThe call passes two values but the procedure has only one parameter. The number of values must match the parameters. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
Debugging — Finding and fixing errors by checking a subroutine's header, parameter types, RETURN, CALL, end keyword and variable scope against the syntax rules.FUNCTION Double(N : INTEGER) RETURNS INTEGER
RETURN N * 2
ENDFUNCTIONFUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTIONPROCEDURE Add(A, B : INTEGER)
CALL Add(5, 7)PROCEDURE ShowTotal()
Total ← 10 + 5
OUTPUT TotalFUNCTION IsAdult(Age : INTEGER) RETURNS
BOOLEAN
RETURN Age ≥ 18
ENDFUNCTION