Pseudocode FUNCTION ... RETURNS ... RETURN ... ENDFUNCTION

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

Pseudocode FUNCTION ... RETURNS ... RETURN ... ENDFUNCTION

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which header correctly starts a Cambridge pseudocode function that takes a whole number and gives back a whole number?
A FUNCTION Double(Num : INTEGER) RETURNS INTEGER
B FUNCTION Double(Num : INTEGER) INTEGER
2. Which line sends a calculated value back from a function called Square?
A RETURN Num * Num
B OUTPUT Num * Num
3. How should a function definition be closed?
A ENDPROCEDURE
B ENDFUNCTION
4. Which statement about functions is true?
A A function never sends a value back
B A function sends a value back using RETURN
5. Which is the correct way to use the function Double inside an expression?
A Total ← Double(5) + 1
B CALL Double(5) + 1

Match the Pairs

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

Left columnRight column
1FUNCTION Double(Num : INTEGER) RETURNS INTEGER ASends the calculated value Num multiplied by 2 back to the part of the program that called the function
2RETURN Num * 2 BStarts a function called Double that takes an integer parameter Num and will send back an integer value
3A function is closed with ENDFUNCTION after its RETURN line, and the function body is indented CThe header is missing RETURNS INTEGER, so the type of value sent back is not stated
4A function call can be used inside an expression because it gives back a value DFUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
5FUNCTION Double(Num : INTEGER) RETURN Num * 2 ENDFUNCTION EOUTPUT is used instead of RETURN, so the value is only displayed and not sent back to the caller
6FUNCTION Double(Num : INTEGER) RETURNS INTEGER OUTPUT Num * 2 ENDFUNCTION FTotal ← Square(5) + 10

Definitions

FUNCTION Name(Param : TYPE) RETURNS TYPE

Fill in the Blanks

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

1.
________ Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
2.
FUNCTION Half(N : _______) RETURNS REAL RETURN N / 2 ENDFUNCTION
3.
FUNCTION Cube(N : INTEGER) _______ INTEGER RETURN N * N * N ENDFUNCTION
4.
FUNCTION AddVAT(P : REAL) RETURNS REAL ______ P * 1.2 ENDFUNCTION
5.
INPUT Price Total ← AddVAT(_____) + 5 OUTPUT Total

Spot the Error

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

Question 1
A FUNCTION Sq(N INTEGER)
B RETURNS INTEGER
C RETURN N * N
D ENDFUNCTION
E OUTPUT Sq(3)
Question 2
A FUNCTION Half(N : REAL)
B RETURN REAL
C RETURN N / 2
D ENDFUNCTION
Question 3
A FUNCTION Cube(N : INTEGER)
B RETURNS STRING
C RETURN N * N * N
D ENDFUNCTION
E OUTPUT Cube(3)
Question 4
A PROCEDURE Tax(P : REAL)
B RETURNS REAL
C RETURN P * 0.2
D ENDFUNCTION
Question 5
A FUNCTION Cost(P : REAL)
B RETURNS REAL
C OUTPUT P * 1.2
D ENDFUNCTION
E OUTPUT Cost(10)

Answer Key

Pseudocode FUNCTION ... RETURNS ... RETURN ... ENDFUNCTION
Section A: Quick Questions
Q1
A — FUNCTION Double(Num : INTEGER) RETURNS INTEGER
Q2
A — RETURN Num * Num
Q3
B — ENDFUNCTION
Q4
B — A function sends a value back using RETURN
Q5
A — Total ← Double(5) + 1
Section B: Match the Pairs
Q1
FUNCTION Double(Num : INTEGER) RETURNS INTEGER → Starts a function called Double that takes an integer parameter Num and will send back an integer value
Q2
RETURN Num * 2 → Sends the calculated value Num multiplied by 2 back to the part of the program that called the function
Q3
A function is closed with ENDFUNCTION after its RETURN line, and the function body is indented → FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q4
A function call can be used inside an expression because it gives back a value → Total ← Square(5) + 10
Q5
FUNCTION Double(Num : INTEGER) RETURN Num * 2 ENDFUNCTION → The header is missing RETURNS INTEGER, so the type of value sent back is not stated
Q6
FUNCTION Double(Num : INTEGER) RETURNS INTEGER OUTPUT Num * 2 ENDFUNCTION → OUTPUT is used instead of RETURN, so the value is only displayed and not sent back to the caller
Section C: Definitions
Q1
FUNCTION Name(Param : TYPE) RETURNS TYPE — Starts a function that takes a typed parameter and RETURNS a value of the stated type. RETURN sends the value back, ENDFUNCTION closes it, and you can call it inside an expression.
Section D: Fill in the Blanks
Q1
FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q2
FUNCTION Half(N : INTEGER) RETURNS REAL RETURN N / 2 ENDFUNCTION
Q3
FUNCTION Cube(N : INTEGER) RETURNS INTEGER RETURN N * N * N ENDFUNCTION
Q4
FUNCTION AddVAT(P : REAL) RETURNS REAL RETURN P * 1.2 ENDFUNCTION
Q5
INPUT Price Total ← AddVAT(Price) + 5 OUTPUT Total
Section E: Spot the Error
Q1
A — A parameter must be written as Name : TYPE. The colon is missing, so it should be FUNCTION Sq(N : INTEGER).
Q2
B — The function header states the type with the keyword RETURNS. RETURN is only used inside the body to send a value back. It should be RETURNS REAL.
Q3
B — The function returns the result of a multiplication, which is a number. The RETURNS type must match the value returned, so it should be RETURNS INTEGER.
Q4
A — This subroutine returns a value and ends with ENDFUNCTION, so it must start with FUNCTION, not PROCEDURE. A procedure does not return a value.
Q5
C — OUTPUT only displays the value. It does not send it back to the caller. A function must use RETURN P * 1.2 to give its result back.