Calling a function inside an expression

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

Calling a function inside an expression

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. SumSquare is a function. Which line calls it correctly inside an expression?
A Total ← SumSquare(10, 20) + 1
B Total ← CALL SumSquare(10, 20) + 1
2. Which statement about calling a function is correct?
A It is used as part of an expression, such as OUTPUT SumSquare(10, 20)
B It is a complete statement on its own, such as SumSquare(10, 20)
3. SumSquare(10, 20) returns 500. What value does SumSquare(10, 20) + 1 give?
A 501
B 31
4. What happens when the computer works out SumSquare(10, 20) + 1?
A The +1 is added to the 10 and 20 first, then the function runs
B The returned value replaces the call, then the rest of the expression is worked out
5. Do you write the CALL keyword when using a function?
A Yes, every subroutine must start with CALL
B No, you just write the function name and values in brackets

Match the Pairs

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

Left columnRight column
1Total ← SumSquare(10, 20) + 1 AScore ← Double(5) + 10
2Answer ← 2 * Cube(3) BCALL is used with a function. CALL is only for procedures. A function call is written inside the expression without CALL: Total ← SumSquare(10, 20) + 1.
3A function is called by writing its name with the values in brackets. No CALL keyword is used. CCube is called with 3. The returned value replaces Cube(3), then it is multiplied by 2 and stored in Answer.
4A function call is not a complete statement on its own. It is placed inside an expression so the returned value is used. DThe function is called with 10 and 20. Its returned value replaces the call, then 1 is added and the answer is stored in Total.
5Total ← CALL SumSquare(10, 20) + 1 EResult ← SumSquare(3, 4)
6SumSquare(10, 20) + 1 FThe function call and calculation are written on their own as a statement, so the returned value is not stored or output. It should be assigned or output, e.g. Total ← SumSquare(10, 20) + 1.

Definitions

SumSquare(10, 20) + 1

Fill in the Blanks

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

1.
Total ← _________(10, 20) + 1
2.
Result ← 2 _ SumSquare(1, 2)
3.
Total _ SumSquare(10, 20) + 1
4.
IF IsEven(8) = TRUE ____ OUTPUT "Even" ENDIF
5.
______ SumSquare(10, 20) + 1

Spot the Error

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

Question 1
A FUNCTION Sq(N) RETURNS INTEGER
B RETURN N * N
C ENDFUNCTION
D Total ← CALL Sq(5) + 1
E OUTPUT Total
Question 2
A Price ← 20
B Tax ← 5
C Cost(Price, Tax) + 1
Question 3
A A ← 10
B B ← 20
C Result ← SumSquare A, B + 1
D OUTPUT Result
Question 4
A Length ← 6
B Width ← 4
C OUTPUT "Working out area"
D OUTPUT Rect(Length) + 10
Question 5
A Double(4) * 3 ← Total
B OUTPUT Total
C OUTPUT Total + 1

Answer Key

Calling a function inside an expression
Section A: Quick Questions
Q1
A — Total ← SumSquare(10, 20) + 1
Q2
A — It is used as part of an expression, such as OUTPUT SumSquare(10, 20)
Q3
A — 501
Q4
B — The returned value replaces the call, then the rest of the expression is worked out
Q5
B — No, you just write the function name and values in brackets
Section B: Match the Pairs
Q1
Total ← SumSquare(10, 20) + 1 → The function is called with 10 and 20. Its returned value replaces the call, then 1 is added and the answer is stored in Total.
Q2
Answer ← 2 * Cube(3) → Cube is called with 3. The returned value replaces Cube(3), then it is multiplied by 2 and stored in Answer.
Q3
A function is called by writing its name with the values in brackets. No CALL keyword is used. → Result ← SumSquare(3, 4)
Q4
A function call is not a complete statement on its own. It is placed inside an expression so the returned value is used. → Score ← Double(5) + 10
Q5
Total ← CALL SumSquare(10, 20) + 1 → CALL is used with a function. CALL is only for procedures. A function call is written inside the expression without CALL: Total ← SumSquare(10, 20) + 1.
Q6
SumSquare(10, 20) + 1 → The function call and calculation are written on their own as a statement, so the returned value is not stored or output. It should be assigned or output, e.g. Total ← SumSquare(10, 20) + 1.
Section C: Definitions
Q1
SumSquare(10, 20) + 1 — A function call is written inside an expression, with no CALL keyword. The returned value replaces the call, then the rest of the expression is worked out.
Section D: Fill in the Blanks
Q1
Total ← SumSquare(10, 20) + 1
Q2
Result ← 2 * SumSquare(1, 2)
Q3
Total ← SumSquare(10, 20) + 1
Q4
IF IsEven(8) = TRUE THEN OUTPUT "Even" ENDIF
Q5
OUTPUT SumSquare(10, 20) + 1
Section E: Spot the Error
Q1
D — CALL is only used with procedures. A function is called by writing its name and brackets inside the expression, so it should be Total ← Sq(5) + 1.
Q2
C — A function call inside an expression is not a complete statement. The value must be used, for example with OUTPUT or by storing it in a variable: OUTPUT Cost(Price, Tax) + 1.
Q3
C — The arguments of a function call must be inside brackets after the function name. It should be Result ← SumSquare(A, B) + 1.
Q4
D — Rect needs two values (Length and Width). The call must pass every argument, so it should be OUTPUT Rect(Length, Width) + 10.
Q5
A — The variable being assigned to goes on the left of the arrow. The function call is part of the expression on the right: Total ← Double(4) * 3.