Finding and fixing errors in subroutine code

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

Finding and fixing errors in subroutine code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A function begins FUNCTION Double(N : INTEGER) RETURNS INTEGER. Its last line is RETURN N * 2 and nothing follows it. What is the error?
A CALL is missing
B ENDFUNCTION is missing
2. FUNCTION Total(A : INTEGER, B : INTEGER) RETURNS INTEGER has the line Sum <- A + B, then ENDFUNCTION. What is wrong?
A The parameters must be REAL
B There is no RETURN statement
3. PROCEDURE Greet(Name : STRING, Age : INTEGER) is called with CALL Greet("Sam"). What is the problem?
A Wrong number of values: 2 are needed but 1 is given
B CALL should be replaced by RETURN
4. Total is declared inside PROCEDURE Add. A line outside Add uses OUTPUT Total. Why does this cause an error?
A Total is local, so it cannot be used outside Add
B OUTPUT cannot display an INTEGER
5. Greet has the header PROCEDURE Greet(Name : STRING, Age : INTEGER). Which call is correct?
A CALL Greet("Sam", 15)
B CALL Greet(15, "Sam")

Match the Pairs

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

Left columnRight 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.

Definitions

Debugging

Fill in the Blanks

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

1.
FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 ___________
2.
FUNCTION Square(N : INTEGER) RETURNS INTEGER ______ N * N ENDFUNCTION
3.
PROCEDURE Add(A, B : INTEGER) ____ Add(5, 7)
4.
PROCEDURE ShowTotal() Total ← 10 + 5 OUTPUT _____
5.
FUNCTION IsAdult(Age : INTEGER) _______ BOOLEAN RETURN Age ≥ 18 ENDFUNCTION

Spot the Error

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

Question 1
A FUNCTION Six() INTEGER
B RETURN 6
C ENDFUNCTION
Question 2
A PROCEDURE Greet(Name)
B OUTPUT "Hi ", Name
C ENDPROCEDURE
Question 3
A FUNCTION Rate() RETURNS REAL
B RETURN 0.5
C ENDPROCEDURE
Question 4
A FUNCTION Ten() RETURNS INTEGER
B Value <- 5
C Value <- Value * 2
D OUTPUT Value
E ENDFUNCTION
Question 5
A FUNCTION Ok() RETURNS BOOLEAN
B RETURN "Yes"
C ENDFUNCTION

Answer Key

Finding and fixing errors in subroutine code
Section A: Quick Questions
Q1
B — ENDFUNCTION is missing
Q2
B — There is no RETURN statement
Q3
A — Wrong number of values: 2 are needed but 1 is given
Q4
A — Total is local, so it cannot be used outside Add
Q5
A — CALL Greet("Sam", 15)
Section B: Match the Pairs
Q1
FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION → This function has no RETURN statement, so it never sends a value back to the caller. Add RETURN N * 2.
Q2
CALL ShowTotal(5, 10) // header: PROCEDURE ShowTotal(A : INTEGER) → The call passes two values but the procedure has only one parameter. The number of values must match the parameters.
Q3
Every FUNCTION must be closed with ENDFUNCTION, and every PROCEDURE with ENDPROCEDURE → FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q4
A local variable only exists inside the subroutine that declares it, so the value must be returned or passed out → FUNCTION AddTwo(N : INTEGER) RETURNS INTEGER DECLARE Result : INTEGER Result ← N + 2 RETURN Result ENDFUNCTION
Q5
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18 → The ENDFUNCTION keyword is missing, so the function is never closed. Add ENDFUNCTION on the last line.
Q6
PROCEDURE Total() DECLARE Sum : INTEGER Sum ← 5 + 3 ENDPROCEDURE OUTPUT Sum → Sum is a local variable declared inside the procedure, so it cannot be used in OUTPUT outside it. Output it inside the procedure.
Section C: Definitions
Q1
Debugging — Finding and fixing errors by checking a subroutine's header, parameter types, RETURN, CALL, end keyword and variable scope against the syntax rules.
Section D: Fill in the Blanks
Q1
FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q2
FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q3
PROCEDURE Add(A, B : INTEGER) CALL Add(5, 7)
Q4
PROCEDURE ShowTotal() Total ← 10 + 5 OUTPUT Total
Q5
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age ≥ 18 ENDFUNCTION
Section E: Spot the Error
Q1
A — The function header is missing the RETURNS keyword. A function header must state the return type as RETURNS INTEGER.
Q2
A — The parameter Name has no data type. Each parameter in the header needs a type, so it should be Name : STRING.
Q3
C — A function must finish with ENDFUNCTION. ENDPROCEDURE is the wrong end keyword, so the function is never closed correctly.
Q4
D — A function must send a value back with RETURN. OUTPUT only displays the value, so the function returns nothing. It should be RETURN Value.
Q5
B — The function returns a BOOLEAN, but "Yes" is a STRING. The returned value must match the return type, so it should be RETURN TRUE.