Common mistakes when calling procedures and functions

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

Common mistakes when calling procedures and functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. SumSquare is a FUNCTION that returns a number. Which line calls it correctly?
A Total ← SumSquare(2, 3)
B CALL SumSquare(2, 3)
2. Greet is a PROCEDURE. Which line calls it correctly?
A CALL Greet("Sam")
B Name ← Greet("Sam")
3. Double is a FUNCTION. What happens when you write Double(5) alone on a line?
A The result is printed on the screen automatically
B The result is thrown away and not used
4. Which line makes use of the value returned by the function Double?
A Double(5)
B OUTPUT Double(5)
5. PrintLine is a PROCEDURE. Which statement is true?
A It does not return a value, so it cannot be used in an expression
B It returns a value, so X ← PrintLine() is fine

Match the Pairs

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

Left columnRight column
1CALL SumSquare(2, 3) ACorrect use of a function call inside an expression. The returned value is used in the calculation and stored in Total.
2Total ← AddFive(4) + 1 BWrong when SumSquare is a function. CALL is only for procedures, so a function must be used in an expression, in OUTPUT or stored in a variable.
3A function call on a line by itself throws away the returned value, so the result is lost. CThe ENDFUNCTION statement is missing. Every function must finish with ENDFUNCTION, and every procedure with ENDPROCEDURE.
4A procedure does not return a value, so it must be run with CALL and not used inside an expression. DShowTotal is a procedure, so it returns no value. It cannot be used in an expression. Use CALL ShowTotal(5) on its own, or make it a function.
5Answer ← ShowTotal(5) + 1 ECALL ShowTotal(5)
6FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 FSumSquare(2, 3)

Definitions

CALL SumSquare(2, 3)

Fill in the Blanks

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

1.
____ ShowScore(5)
2.
Total ← _________(2, 3)
3.
______ SumSquare(2, 3)
4.
PROCEDURE ShowScore(Score : INTEGER) OUTPUT Score ____________
5.
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ___________

Spot the Error

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

Question 1
A DECLARE R : INTEGER
B CALL Sq(5)
C OUTPUT R
D FUNCTION Sq(N) RETURNS INTEGER
E RETURN N * N
F ENDFUNCTION
Question 2
A N ← 7
B Sq(N)
C FUNCTION Sq(N) RETURNS INTEGER
D RETURN N * N
E ENDFUNCTION
Question 3
A N ← 4
B Total ← Show(N)
C PROCEDURE Show(N : INTEGER)
D OUTPUT N * 2
E ENDPROCEDURE
Question 4
A PROCEDURE Cube(N) RETURNS REAL
B RETURN N * N * N
C ENDFUNCTION
D OUTPUT Cube(2)
Question 5
A FUNCTION Sq(N) RETURNS INTEGER
B RETURN N * N
C ENDFUNCTION
D R ← CALL Sq(3)
E OUTPUT R

Answer Key

Common mistakes when calling procedures and functions
Section A: Quick Questions
Q1
A — Total ← SumSquare(2, 3)
Q2
A — CALL Greet("Sam")
Q3
B — The result is thrown away and not used
Q4
B — OUTPUT Double(5)
Q5
A — It does not return a value, so it cannot be used in an expression
Section B: Match the Pairs
Q1
CALL SumSquare(2, 3) → Wrong when SumSquare is a function. CALL is only for procedures, so a function must be used in an expression, in OUTPUT or stored in a variable.
Q2
Total ← AddFive(4) + 1 → Correct use of a function call inside an expression. The returned value is used in the calculation and stored in Total.
Q3
A function call on a line by itself throws away the returned value, so the result is lost. → SumSquare(2, 3)
Q4
A procedure does not return a value, so it must be run with CALL and not used inside an expression. → CALL ShowTotal(5)
Q5
Answer ← ShowTotal(5) + 1 → ShowTotal is a procedure, so it returns no value. It cannot be used in an expression. Use CALL ShowTotal(5) on its own, or make it a function.
Q6
FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 → The ENDFUNCTION statement is missing. Every function must finish with ENDFUNCTION, and every procedure with ENDPROCEDURE.
Section C: Definitions
Q1
CALL SumSquare(2, 3) — A common error when SumSquare is a function. CALL is only for procedures. A function returns a value, so use it in an expression, in OUTPUT, or store it in a variable.
Section D: Fill in the Blanks
Q1
CALL ShowScore(5)
Q2
Total ← SumSquare(2, 3)
Q3
OUTPUT SumSquare(2, 3)
Q4
PROCEDURE ShowScore(Score : INTEGER) OUTPUT Score ENDPROCEDURE
Q5
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Section E: Spot the Error
Q1
B — Sq is a function, and CALL is only for procedures. A function returns a value, so its result must be stored or used. The line should be R ← Sq(5).
Q2
B — A function call alone on a line throws away the value it returns, so nothing is shown. The result must be used, for example OUTPUT Sq(N).
Q3
B — Show is a procedure, so it does not return a value and cannot be used in an assignment. Procedures are run with CALL, so the line should be CALL Show(N).
Q4
A — A subroutine that returns a value is a function, not a procedure. The header should start with FUNCTION. A procedure never has a RETURNS type.
Q5
D — CALL must not be used with a function. A function is called inside an expression, so the line should be R ← Sq(3).