Storing a function result in a variable

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

Storing a function result in a variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores the value returned by SumSquare in the variable Total?
A SumSquare(3, 4) ← Total
B Total ← SumSquare(3, 4)
2. SumSquare(A, B) returns A * A + B * B. After Total ← SumSquare(3, 4), what is stored in Total?
A 25
B 7
3. SumSquare is declared with RETURNS INTEGER. Which declaration suits the variable that stores its result?
A DECLARE Total : INTEGER
B DECLARE Total : STRING
4. After Total ← SumSquare(3, 4), what does OUTPUT Total display?
A The returned value, 25
B The text SumSquare(3, 4)
5. After Total ← SumSquare(3, 4), how can you use the result again later in the program?
A Call SumSquare(3, 4) again every time you need the value
B Use the variable Total again without calling the function

Match the Pairs

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

Left columnRight column
1Total ← SumSquare(3, 4) ADECLARE Total : INTEGER Total ← SumSquare(3, 4)
2OUTPUT Total BCalls SumSquare with 3 and 4, then stores the value it returns in the variable Total.
3The variable that stores a returned value must have the same type as the function's return type, so a function that returns INTEGER is stored in an INTEGER variable. CThe returned value is never stored. Total has no value because the call is not assigned, so it should be Total ← SumSquare(3, 4).
4Calling the same function twice with different values gives two results, which can be stored in two different variables. DFirst ← SumSquare(3, 4) Second ← SumSquare(5, 6)
5SumSquare(3, 4) ← Total EUses the stored result later in the program by displaying the value held in Total.
6SumSquare(3, 4) OUTPUT Total FThe arrow points the wrong way. The variable must be on the left and the function call on the right, as in Total ← SumSquare(3, 4).

Definitions

Total ← SumSquare(3, 4)

Fill in the Blanks

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

1.
Total ← _________(3, 4)
2.
Total _ SumSquare(3, 4)
3.
Total ← SumSquare(3, 4) OUTPUT _____
4.
DECLARE Total : _______ Total ← SumSquare(3, 4)
5.
Total1 ← SumSquare(3, 4) Total2 ← _________(5, 6) OUTPUT Total1 + Total2

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B SumSquare(3, 4) ← Total
C OUTPUT Total
Question 2
A DECLARE Area : INTEGER
B Area ← RectArea(5, 2)
C OUTPUT "Area is ", Aera
Question 3
A DECLARE Total : STRING
B Total ← SumSquare(3, 4)
C OUTPUT Total
Question 4
A DECLARE Ok : BOOLEAN
B Ok ← CALL IsEven(8)
C IF Ok = TRUE THEN
D OUTPUT "Even"
Question 5
A DECLARE A, B : INTEGER
B A ← SumSquare(1)
C B ← SumSquare(3, 4)
D OUTPUT A + B

Answer Key

Storing a function result in a variable
Section A: Quick Questions
Q1
B — Total ← SumSquare(3, 4)
Q2
A — 25
Q3
A — DECLARE Total : INTEGER
Q4
A — The returned value, 25
Q5
B — Use the variable Total again without calling the function
Section B: Match the Pairs
Q1
Total ← SumSquare(3, 4) → Calls SumSquare with 3 and 4, then stores the value it returns in the variable Total.
Q2
OUTPUT Total → Uses the stored result later in the program by displaying the value held in Total.
Q3
The variable that stores a returned value must have the same type as the function's return type, so a function that returns INTEGER is stored in an INTEGER variable. → DECLARE Total : INTEGER Total ← SumSquare(3, 4)
Q4
Calling the same function twice with different values gives two results, which can be stored in two different variables. → First ← SumSquare(3, 4) Second ← SumSquare(5, 6)
Q5
SumSquare(3, 4) ← Total → The arrow points the wrong way. The variable must be on the left and the function call on the right, as in Total ← SumSquare(3, 4).
Q6
SumSquare(3, 4) OUTPUT Total → The returned value is never stored. Total has no value because the call is not assigned, so it should be Total ← SumSquare(3, 4).
Section C: Definitions
Q1
Total ← SumSquare(3, 4) — The function runs, and the value it returns is stored in the variable on the left of the arrow. You can use that variable later. Its type must match the function's return type.
Section D: Fill in the Blanks
Q1
Total ← SumSquare(3, 4)
Q2
Total ← SumSquare(3, 4)
Q3
Total ← SumSquare(3, 4) OUTPUT Total
Q4
DECLARE Total : INTEGER Total ← SumSquare(3, 4)
Q5
Total1 ← SumSquare(3, 4) Total2 ← SumSquare(5, 6) OUTPUT Total1 + Total2
Section E: Spot the Error
Q1
B — The arrow assigns the value on its right to the variable on its left. The variable must be on the left: Total ← SumSquare(3, 4).
Q2
C — The variable that stores the returned value is Area. Aera is a misspelled name that was never assigned. It should be OUTPUT "Area is ", Area.
Q3
A — SumSquare returns an INTEGER, so the variable that stores the result must be INTEGER too. It should be DECLARE Total : INTEGER.
Q4
B — CALL is only used for procedures. A function is called by its name inside the expression: Ok ← IsEven(8).
Q5
B — SumSquare needs two arguments, so the call must give two values. It should be A ← SumSquare(1, 2).