Writing exam answers about procedures and functions

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

Writing exam answers about procedures and functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the correct Cambridge pseudocode header for a function that gives back a whole number?
A FUNCTION Double(n : INTEGER) RETURN INTEGER
B FUNCTION Double(n : INTEGER) RETURNS INTEGER
2. Which line correctly ends a function?
A ENDFUNCTION
B ENDPROCEDURE
3. Which statement inside a function body sends the value back to the caller?
A RETURNS Total
B RETURN Total
4. Which is the correct way to use the function Area in a program?
A OUTPUT Area(5.0)
B CALL Area(5.0)
5. Which exam sentence defines a function correctly?
A A subroutine that returns a value to the part of the program that called it.
B A subroutine that never sends a value back to the program.

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 AThe header of a function: RETURNS gives the data type of the value sent back to the caller
2ENDPROCEDURE BCALL ShowMenu()
3A procedure is run with the CALL keyword, and it does not return a value Ctotal ← AddTax(price)
4A function sends a value back with RETURN, so its call is used inside an expression, not with CALL DThe header is missing RETURNS REAL, so the data type of the returned value is not stated.
5CALL Area(5.0) EThe keyword that closes a procedure; a function is closed with ENDFUNCTION instead
6FUNCTION Area(r : REAL) RETURN r * r * 3.14 ENDFUNCTION FCALL is used with a function. Functions return a value, so the call should be used in an expression such as result ← Area(5.0).

Definitions

RETURNS

Fill in the Blanks

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

1.
FUNCTION Area(r : REAL) _______ REAL
2.
FUNCTION IsEven(n : INTEGER) RETURNS _______ RETURN n MOD 2 = 0 ENDFUNCTION
3.
PROCEDURE ShowScore(s : INTEGER) OUTPUT s ____________
4.
FUNCTION Square(n : INTEGER) RETURNS INTEGER ______ n * n ENDFUNCTION
5.
____ ShowScore(85)

Spot the Error

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

Question 1
A FUNCTION Pi() RETURN REAL
B RETURN 3.14
C ENDFUNCTION
Question 2
A PROCEDURE ShowMenu()
B OUTPUT "1. Play"
C ENDFUNCTION
Question 3
A DECLARE score : INTEGER
B score ← 0
C score ← CALL GetScore()
D OUTPUT score
Question 4
A FUNCTION Pi() RETURNS REAL
B OUTPUT 3.14
C ENDFUNCTION
Question 5
A PROCEDURE Greet(n : STRING)
B OUTPUT "Hi ", n
C ENDPROCEDURE
D Greet("Sam")

Answer Key

Writing exam answers about procedures and functions
Section A: Quick Questions
Q1
B — FUNCTION Double(n : INTEGER) RETURNS INTEGER
Q2
A — ENDFUNCTION
Q3
B — RETURN Total
Q4
A — OUTPUT Area(5.0)
Q5
A — A subroutine that returns a value to the part of the program that called it.
Section B: Match the Pairs
Q1
FUNCTION Double(n : INTEGER) RETURNS INTEGER → The header of a function: RETURNS gives the data type of the value sent back to the caller
Q2
ENDPROCEDURE → The keyword that closes a procedure; a function is closed with ENDFUNCTION instead
Q3
A procedure is run with the CALL keyword, and it does not return a value → CALL ShowMenu()
Q4
A function sends a value back with RETURN, so its call is used inside an expression, not with CALL → total ← AddTax(price)
Q5
CALL Area(5.0) → CALL is used with a function. Functions return a value, so the call should be used in an expression such as result ← Area(5.0).
Q6
FUNCTION Area(r : REAL) RETURN r * r * 3.14 ENDFUNCTION → The header is missing RETURNS REAL, so the data type of the returned value is not stated.
Section C: Definitions
Q1
RETURNS — Keyword in a FUNCTION header giving the data type of the value sent back, e.g. FUNCTION Area(r : REAL) RETURNS REAL. End with ENDFUNCTION. Never use CALL for a function.
Section D: Fill in the Blanks
Q1
FUNCTION Area(r : REAL) RETURNS REAL
Q2
FUNCTION IsEven(n : INTEGER) RETURNS BOOLEAN RETURN n MOD 2 = 0 ENDFUNCTION
Q3
PROCEDURE ShowScore(s : INTEGER) OUTPUT s ENDPROCEDURE
Q4
FUNCTION Square(n : INTEGER) RETURNS INTEGER RETURN n * n ENDFUNCTION
Q5
CALL ShowScore(85)
Section E: Spot the Error
Q1
A — The function header must use the keyword RETURNS (with an S) before the data type. It should be FUNCTION Pi() RETURNS REAL. RETURN is only used inside the body to send the value back.
Q2
C — A procedure must be closed with ENDPROCEDURE, not ENDFUNCTION. Each block ends with the keyword that matches the one that started it.
Q3
C — CALL is only used for procedures. A function is used in an expression, so the line should be score ← GetScore().
Q4
B — A function must send its value back with RETURN. OUTPUT only displays a value and returns nothing. It should be RETURN 3.14.
Q5
D — A procedure must be run using the keyword CALL. It should be CALL Greet("Sam").