Defining a function with no parameters

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

Defining a function with no parameters

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which header correctly starts a function called GetPi that has no parameters and returns a REAL?
A FUNCTION GetPi RETURNS REAL
B FUNCTION GetPi() RETURNS REAL
2. What closes a function definition?
A ENDFUNCTION
B ENDPROCEDURE
3. In FUNCTION GetPi RETURNS REAL, what does REAL tell us?
A The data type of the value the function sends back
B The data type of a parameter passed into the function
4. Which word must come between the function name and the data type?
A RETURN
B RETURNS
5. Which function definition is written correctly?
A FUNCTION GetPi RETURNS REAL RETURN 3.14 ENDPROCEDURE
B FUNCTION GetPi RETURNS REAL RETURN 3.14 ENDFUNCTION

Match the Pairs

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

Left columnRight column
1FUNCTION GetPi RETURNS REAL AFUNCTION GetPi RETURNS REAL RETURN 3.14 ENDFUNCTION
2ENDFUNCTION BCloses the function definition after its statements
3A function with no parameters has no brackets after its name, then RETURNS and the data type CFUNCTION GetPi RETURNS REAL
4A complete function with no parameters returns a value before ENDFUNCTION DStarts a function with no parameters that sends back a REAL value
5FUNCTION GetPi() RETURNS REAL EEmpty brackets were added after the name. A function with no parameters is written without brackets, so it should be FUNCTION GetPi RETURNS REAL.
6FUNCTION GetPi RETURN 3.14 ENDFUNCTION FThe RETURNS keyword and data type are missing from the header. It should be FUNCTION GetPi RETURNS REAL.

Definitions

FUNCTION GetPi RETURNS REAL

Fill in the Blanks

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

1.
________ GetPi RETURNS REAL RETURN 3.14159 ENDFUNCTION
2.
FUNCTION GetPi _______ REAL RETURN 3.14159 ENDFUNCTION
3.
FUNCTION IsOpen RETURNS _______ RETURN TRUE ENDFUNCTION
4.
FUNCTION GetLimit RETURNS INTEGER RETURN 100 ___________
5.
FUNCTION GetPi RETURNS REAL ______ 3.14159 ENDFUNCTION

Spot the Error

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

Question 1
A FUNCTION GetPi() RETURNS REAL
B RETURN 3.14159
C ENDFUNCTION
Question 2
A FUNCTION GetMax INTEGER
B RETURN 100
C ENDFUNCTION
Question 3
A FUNCTION GetTen RETURNS INTEGER
B Total ← 10
C RETURN Total
D ENDPROCEDURE
Question 4
A FUNCTION GetName RETURNS STRING
B Name ← "Sam"
C OUTPUT Name
D ENDFUNCTION
Question 5
A FUNCTION GetPi RETURNS REAL
B RETURN "3.14159"
C ENDFUNCTION

Answer Key

Defining a function with no parameters
Section A: Quick Questions
Q1
A — FUNCTION GetPi RETURNS REAL
Q2
A — ENDFUNCTION
Q3
A — The data type of the value the function sends back
Q4
B — RETURNS
Q5
B — FUNCTION GetPi RETURNS REAL RETURN 3.14 ENDFUNCTION
Section B: Match the Pairs
Q1
FUNCTION GetPi RETURNS REAL → Starts a function with no parameters that sends back a REAL value
Q2
ENDFUNCTION → Closes the function definition after its statements
Q3
A function with no parameters has no brackets after its name, then RETURNS and the data type → FUNCTION GetPi RETURNS REAL
Q4
A complete function with no parameters returns a value before ENDFUNCTION → FUNCTION GetPi RETURNS REAL RETURN 3.14 ENDFUNCTION
Q5
FUNCTION GetPi() RETURNS REAL → Empty brackets were added after the name. A function with no parameters is written without brackets, so it should be FUNCTION GetPi RETURNS REAL.
Q6
FUNCTION GetPi RETURN 3.14 ENDFUNCTION → The RETURNS keyword and data type are missing from the header. It should be FUNCTION GetPi RETURNS REAL.
Section C: Definitions
Q1
FUNCTION GetPi RETURNS REAL — Starts a function with no parameters: the name has no brackets, RETURNS gives the data type of the value sent back, and ENDFUNCTION closes the function.
Section D: Fill in the Blanks
Q1
FUNCTION GetPi RETURNS REAL RETURN 3.14159 ENDFUNCTION
Q2
FUNCTION GetPi RETURNS REAL RETURN 3.14159 ENDFUNCTION
Q3
FUNCTION IsOpen RETURNS BOOLEAN RETURN TRUE ENDFUNCTION
Q4
FUNCTION GetLimit RETURNS INTEGER RETURN 100 ENDFUNCTION
Q5
FUNCTION GetPi RETURNS REAL RETURN 3.14159 ENDFUNCTION
Section E: Spot the Error
Q1
A — A function with no parameters is written without brackets after its name. It should be FUNCTION GetPi RETURNS REAL.
Q2
A — The header must have the keyword RETURNS before the data type. It should be FUNCTION GetMax RETURNS INTEGER.
Q3
D — A function is closed with ENDFUNCTION, not ENDPROCEDURE. ENDPROCEDURE belongs to procedures.
Q4
C — A function must send its value back with RETURN. OUTPUT only displays it, so the line should be RETURN Name.
Q5
B — The function RETURNS REAL, so it must return a number. The quotes make it a STRING. It should be RETURN 3.14159.