Defining a function with parameters

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

Defining a function with parameters

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function header is written correctly?
A FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
B FUNCTION SumSquare(INTEGER Number1, INTEGER Number2) RETURNS INTEGER
2. Where do the parameters of a function go?
A In brackets straight after the function name
B After the word RETURNS
3. Which header correctly shows the return type?
A FUNCTION Double(Num:INTEGER) RETURN INTEGER
B FUNCTION Double(Num:INTEGER) RETURNS INTEGER
4. Which function body is correct?
A RETURN Number1 * Number1 + Number2 * Number2 ENDFUNCTION
B RETURN Number1 * Number1 + Number2 * Number2 ENDPROCEDURE
5. How should a function with two parameters separate them in the header?
A Number1, Number2:INTEGER
B Number1:INTEGER, Number2:INTEGER

Match the Pairs

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

Left columnRight column
1FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER AThe header is missing RETURNS and the return data type after the brackets, for example RETURNS REAL.
2RETURN Number1 * Number1 + Number2 * Number2 BThe parameters have no data types. Each one must be written as name:TYPE, for example Number1:INTEGER.
3A function with one parameter: name, colon, data type in brackets, then RETURNS and the return type CFUNCTION Double(Num:INTEGER) RETURNS INTEGER
4A function definition with parameters always finishes with ENDFUNCTION after the RETURN statement DUses both parameters in the RETURN expression to calculate the value sent back
5FUNCTION SumSquare(Number1, Number2) RETURNS INTEGER EHeader of a function with two INTEGER parameters that sends back an INTEGER result
6FUNCTION Average(A:INTEGER, B:INTEGER) RETURN (A + B) / 2 ENDFUNCTION FFUNCTION AddThree(A:INTEGER, B:INTEGER, C:INTEGER) RETURNS INTEGER RETURN A + B + C ENDFUNCTION

Definitions

FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER

Fill in the Blanks

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

1.
________ Double(N:INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
2.
FUNCTION Double(N:INTEGER) _______ INTEGER RETURN N * 2 ENDFUNCTION
3.
FUNCTION Cube(N:INTEGER) RETURNS INTEGER RETURN N * N * N ___________
4.
FUNCTION Add(A:REAL, B:REAL) RETURNS REAL RETURN A + _ ENDFUNCTION
5.
FUNCTION IsOld(Age:INTEGER) RETURNS _______ RETURN Age >= 18 ENDFUNCTION

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 ENDPROCEDURE
Question 2
A FUNCTION Add(A:REAL, B:REAL)
B RETURNS REAL
C RETURN A + C
D ENDFUNCTION
Question 3
A FUNCTION Twice(INTEGER:N)
B RETURNS INTEGER
C RETURN N * 2
D ENDFUNCTION
Question 4
A FUNCTION Half(N:REAL)
B RETURNS
C RETURN N / 2
D ENDFUNCTION
Question 5
A FUNCTION Area(L:REAL, W:REAL)
B RETURNS REAL
C OUTPUT L * W
D ENDFUNCTION

Answer Key

Defining a function with parameters
Section A: Quick Questions
Q1
A — FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER
Q2
A — In brackets straight after the function name
Q3
B — FUNCTION Double(Num:INTEGER) RETURNS INTEGER
Q4
A — RETURN Number1 * Number1 + Number2 * Number2 ENDFUNCTION
Q5
B — Number1:INTEGER, Number2:INTEGER
Section B: Match the Pairs
Q1
FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER → Header of a function with two INTEGER parameters that sends back an INTEGER result
Q2
RETURN Number1 * Number1 + Number2 * Number2 → Uses both parameters in the RETURN expression to calculate the value sent back
Q3
A function with one parameter: name, colon, data type in brackets, then RETURNS and the return type → FUNCTION Double(Num:INTEGER) RETURNS INTEGER
Q4
A function definition with parameters always finishes with ENDFUNCTION after the RETURN statement → FUNCTION AddThree(A:INTEGER, B:INTEGER, C:INTEGER) RETURNS INTEGER RETURN A + B + C ENDFUNCTION
Q5
FUNCTION SumSquare(Number1, Number2) RETURNS INTEGER → The parameters have no data types. Each one must be written as name:TYPE, for example Number1:INTEGER.
Q6
FUNCTION Average(A:INTEGER, B:INTEGER) RETURN (A + B) / 2 ENDFUNCTION → The header is missing RETURNS and the return data type after the brackets, for example RETURNS REAL.
Section C: Definitions
Q1
FUNCTION SumSquare(Number1:INTEGER, Number2:INTEGER) RETURNS INTEGER — A function header lists each parameter in brackets as name:TYPE, then RETURNS and the return type. The body uses the parameters in a RETURN expression and ends with ENDFUNCTION.
Section D: Fill in the Blanks
Q1
FUNCTION Double(N:INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q2
FUNCTION Double(N:INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q3
FUNCTION Cube(N:INTEGER) RETURNS INTEGER RETURN N * N * N ENDFUNCTION
Q4
FUNCTION Add(A:REAL, B:REAL) RETURNS REAL RETURN A + B ENDFUNCTION
Q5
FUNCTION IsOld(Age:INTEGER) RETURNS BOOLEAN RETURN Age >= 18 ENDFUNCTION
Section E: Spot the Error
Q1
D — A function must finish with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE is only used to close a procedure.
Q2
C — The RETURN expression can only use the parameters in the header. C is not a parameter, so it should be RETURN A + B.
Q3
A — Each parameter is written as identifier, colon, data type. The name comes first, so it should be (N:INTEGER).
Q4
B — RETURNS must be followed by the data type of the value the function gives back. It should be RETURNS REAL.
Q5
C — A function must send its answer back with RETURN. OUTPUT only displays a value and returns nothing. It should be RETURN L * W.