The RETURN statement

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

The RETURN statement

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement sends a value back to the place where a function was called?
A OUTPUT Total
B RETURN Total
2. Which function ends with a correct RETURN of an expression?
A RETURN Number1 * Number1
B RETURN INTEGER Number1 * Number1
3. Where does the RETURN statement normally go in a function?
A At the very start, before the function header
B As the last statement, just before ENDFUNCTION
4. A function has this line: RETURN Result. What does it return?
A The value currently stored in the variable Result
B The word "Result" as text
5. FUNCTION Double(N : INTEGER) RETURNS INTEGER has RETURN N * 2 on line 2 and OUTPUT "Done" on line 3. What happens when Double(5) is called?
A It returns 10 and "Done" is never output
B It returns 10 and then outputs "Done"

Match the Pairs

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

Left columnRight column
1RETURN Number1 * Number1 ARETURN has no value or expression after it, so nothing is sent back. It should be RETURN A + B.
2RETURN Total BFUNCTION Double(Num : INTEGER) RETURNS INTEGER RETURN Num * 2 ENDFUNCTION
3A function ends with RETURN as its last statement, sending back its answer CThe function uses OUTPUT instead of RETURN, so no value is sent back to the caller. It should be RETURN Number1 * Number1.
4RETURN ends the function, so any statement written after it in the same path never runs DRETURN Result OUTPUT "Done"
5FUNCTION Square(Number1 : INTEGER) RETURNS INTEGER OUTPUT Number1 * Number1 ENDFUNCTION ESends the value stored in the variable Total back to the place where the function was called
6FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER RETURN ENDFUNCTION FWorks out the square of Number1 and sends that value back to where the function was called

Definitions

RETURN

Fill in the Blanks

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

1.
FUNCTION Sq(Number1 : REAL) RETURNS REAL ______ Number1 * Number1 ENDFUNCTION
2.
FUNCTION Sq(Number1 : REAL) RETURNS REAL RETURN Number1 _ Number1 ENDFUNCTION
3.
Sum ← A + B RETURN ___ ENDFUNCTION
4.
Result ← Sq(5) OUTPUT ______
5.
FUNCTION Pass(Mark : INTEGER) RETURNS BOOLEAN RETURN Mark __ 50 ENDFUNCTION

Spot the Error

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

Question 1
A FUNCTION Sq(N) RETURNS INTEGER
B Result ← N * N
C RETURN
D ENDFUNCTION
Question 2
A FUNCTION Sq(N) RETURNS INTEGER
B OUTPUT N * N
C ENDFUNCTION
D OUTPUT Sq(5)
Question 3
A PROCEDURE Half(N)
B RETURN N / 2
C ENDFUNCTION
Question 4
A FUNCTION Pay(H) RETURNS REAL
B Total ← H * 12.5
C OUTPUT "Paid"
D RETURN H
E ENDFUNCTION
Question 5
A FUNCTION Dbl(N) RETURNS INTEGER
B Answer ← N * 2
C RETURNS Answer
D ENDFUNCTION
E OUTPUT Dbl(4)

Answer Key

The RETURN statement
Section A: Quick Questions
Q1
B — RETURN Total
Q2
A — RETURN Number1 * Number1
Q3
B — As the last statement, just before ENDFUNCTION
Q4
A — The value currently stored in the variable Result
Q5
A — It returns 10 and "Done" is never output
Section B: Match the Pairs
Q1
RETURN Number1 * Number1 → Works out the square of Number1 and sends that value back to where the function was called
Q2
RETURN Total → Sends the value stored in the variable Total back to the place where the function was called
Q3
A function ends with RETURN as its last statement, sending back its answer → FUNCTION Double(Num : INTEGER) RETURNS INTEGER RETURN Num * 2 ENDFUNCTION
Q4
RETURN ends the function, so any statement written after it in the same path never runs → RETURN Result OUTPUT "Done"
Q5
FUNCTION Square(Number1 : INTEGER) RETURNS INTEGER OUTPUT Number1 * Number1 ENDFUNCTION → The function uses OUTPUT instead of RETURN, so no value is sent back to the caller. It should be RETURN Number1 * Number1.
Q6
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER RETURN ENDFUNCTION → RETURN has no value or expression after it, so nothing is sent back. It should be RETURN A + B.
Section C: Definitions
Q1
RETURN — RETURN followed by a value or expression sends that value back to where the function was called, e.g. RETURN Number1 * Number1. It also ends the function.
Section D: Fill in the Blanks
Q1
FUNCTION Sq(Number1 : REAL) RETURNS REAL RETURN Number1 * Number1 ENDFUNCTION
Q2
FUNCTION Sq(Number1 : REAL) RETURNS REAL RETURN Number1 * Number1 ENDFUNCTION
Q3
Sum ← A + B RETURN Sum ENDFUNCTION
Q4
Result ← Sq(5) OUTPUT Result
Q5
FUNCTION Pass(Mark : INTEGER) RETURNS BOOLEAN RETURN Mark >= 50 ENDFUNCTION
Section E: Spot the Error
Q1
C — RETURN must be followed by a value or expression. A bare RETURN sends nothing back to the call. It should be RETURN Result.
Q2
B — OUTPUT only displays the value. It does not send it back to where the function was called, so Sq(5) has no value. The line should be RETURN N * N.
Q3
A — Only a function can use RETURN to send a value back. A procedure does not return a value. The header should be FUNCTION Half(N) RETURNS REAL.
Q4
D — This returns the hours, not the pay that was worked out. The line should be RETURN Total so the calculated value goes back to the call.
Q5
C — RETURNS belongs in the function header to give the data type. Inside the function the statement is RETURN Answer.