Function

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

Function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement describes a function?
A A named subroutine that returns a single value to where it is called
B A named subroutine that never gives anything back to the program
2. Which line is a correct way to use a function call?
A CALL Total(2, 3) ← Answer
B Answer ← Total(2, 3)
3. How many values does a function return to the place where it is called?
A One value
B Several values at once
4. A function is declared like this: FUNCTION Area(W : INTEGER) RETURNS INTEGER. What does RETURNS INTEGER tell us?
A The parameter W must be typed in by the user
B The value returned has the data type INTEGER
5. Which is valid use of a function call inside an expression?
A Score ← Total(2, 3) + 10
B Score ← CALL Total(2, 3) + 10

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 has no RETURNS data type, so the type of the value that comes back is not stated. It should be RETURNS INTEGER.
2RETURN N * 2 BStarts a function called Double that takes an integer and sends back one INTEGER value
3A function call can be used as part of an expression because it gives back one value CSends one value back to the place where the function was called
4The returned value has a data type that is declared in the function header DFUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
5FUNCTION Double(N : INTEGER) RETURN N * 2 ENDFUNCTION EAnswer ← Double(4) + 1
6FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION FThe function uses OUTPUT instead of RETURN, so no value comes back to the call. It should be RETURN N * 2.

Definitions

FUNCTION

Fill in the Blanks

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

1.
________ Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
2.
FUNCTION Twice(N : INTEGER) _______ INTEGER RETURN N * 2 ENDFUNCTION
3.
FUNCTION Twice(N : INTEGER) RETURNS INTEGER ______ N * 2 ENDFUNCTION
4.
FUNCTION IsBig(N : INTEGER) RETURNS _______ RETURN N > 100 ENDFUNCTION
5.
x _ Total(2, 3) OUTPUT x

Spot the Error

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

Question 1
A FUNCTION Pi() REAL
B RETURN 3.14
C ENDFUNCTION
D OUTPUT Pi()
Question 2
A FUNCTION Half(N) RETURNS REAL
B Result ← N / 2
C OUTPUT Result
D ENDFUNCTION
Question 3
A FUNCTION Sum(A,B) RETURNS REAL
B Result ← A + B
C RETURN Result
D ENDPROCEDURE
E OUTPUT Sum(2,3)
Question 4
A FUNCTION Sq(N) RETURNS REAL
B RETURN N * N
C ENDFUNCTION
D Area ← CALL Sq(5)
Question 5
A FUNCTION Hi() RETURNS STRING
B RETURN 42
C ENDFUNCTION
D OUTPUT Hi()

Answer Key

Function
Section A: Quick Questions
Q1
A — A named subroutine that returns a single value to where it is called
Q2
B — Answer ← Total(2, 3)
Q3
A — One value
Q4
B — The value returned has the data type INTEGER
Q5
A — Score ← Total(2, 3) + 10
Section B: Match the Pairs
Q1
FUNCTION Double(N : INTEGER) RETURNS INTEGER → Starts a function called Double that takes an integer and sends back one INTEGER value
Q2
RETURN N * 2 → Sends one value back to the place where the function was called
Q3
A function call can be used as part of an expression because it gives back one value → Answer ← Double(4) + 1
Q4
The returned value has a data type that is declared in the function header → FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN
Q5
FUNCTION Double(N : INTEGER) RETURN N * 2 ENDFUNCTION → The header has no RETURNS data type, so the type of the value that comes back is not stated. It should be RETURNS INTEGER.
Q6
FUNCTION Double(N : INTEGER) RETURNS INTEGER OUTPUT N * 2 ENDFUNCTION → The function uses OUTPUT instead of RETURN, so no value comes back to the call. It should be RETURN N * 2.
Section C: Definitions
Q1
FUNCTION — A named subroutine that returns a single value to the place where it is called, so the call can be used inside an expression, such as x ← Total(2, 3).
Section D: Fill in the Blanks
Q1
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q2
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q3
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q4
FUNCTION IsBig(N : INTEGER) RETURNS BOOLEAN RETURN N > 100 ENDFUNCTION
Q5
x ← Total(2, 3) OUTPUT x
Section E: Spot the Error
Q1
A — The header must say RETURNS before the data type of the value that comes back. It should be FUNCTION Pi() RETURNS REAL.
Q2
C — A function must send its value back to the call with RETURN. OUTPUT only displays it and nothing comes back. It should be RETURN Result.
Q3
D — A function definition must be closed with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE belongs to procedures.
Q4
D — CALL is only used for procedures. A function is called by its name inside an expression, so it should be Area ← Sq(5).
Q5
B — The function is declared to return a STRING, but 42 is an INTEGER. The returned value must match the declared type, e.g. RETURN "Hello".