Choosing a procedure or a function for a task

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

Choosing a procedure or a function for a task

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A subroutine works out the area of a rectangle and the program uses that number later. Which should you write?
A A procedure, because it produces one value that the program needs
B A function, because it produces one value that the program needs
2. A subroutine only displays a menu with OUTPUT and gives nothing back. Which should you write?
A A procedure, because it only does something
B A function, because it only does something
3. Which statement is correct?
A A function uses RETURN to send a value back to the line that called it
B A procedure uses RETURN to send a value back to the line that called it
4. Which task suits a function?
A Calculating the area of a circle from its radius
B Printing a welcome menu on the screen
5. Which task suits a procedure?
A Calculating the total price of items in a basket
B Using OUTPUT to show a list of menu options

Match the Pairs

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

Left columnRight column
1FUNCTION CalcArea(L : INTEGER, W : INTEGER) RETURNS INTEGER AA procedure cannot return a value. The task produces one value the program needs, so it should be a FUNCTION with RETURNS and ENDFUNCTION.
2PROCEDURE ShowMenu() BCALL ShowMenu()
3A function returns a value to the program, so the result can be stored in a variable. CUse a function because the task produces one value, the area, that the program needs.
4A procedure only carries out actions such as OUTPUT, so it is run with its own CALL statement. DUse a procedure because the task only does something, such as displaying a menu, and gives no value back.
5PROCEDURE CalcArea(L, W) RETURN L * W ENDPROCEDURE EArea ← CalcArea(5, 3)
6FUNCTION ShowMenu() OUTPUT "1. Play" ENDFUNCTION FDisplaying a menu only does something and returns no value, so it should be a PROCEDURE and not a FUNCTION.

Definitions

FUNCTION

Fill in the Blanks

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

1.
_________ ShowMenu() OUTPUT "1. Play" ENDPROCEDURE
2.
FUNCTION Area(L : INTEGER) _______ INTEGER RETURN L * L ENDFUNCTION
3.
FUNCTION Area(L, W) RETURNS INTEGER ______ L * W ENDFUNCTION
4.
____ ShowMenu() area ← Area(4, 5)
5.
area _ Area(3, 6) OUTPUT area

Spot the Error

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

Question 1
A FUNCTION Area(S) RETURNS REAL
B RETURN S * S
C ENDPROCEDURE
D OUTPUT Area(4)
Question 2
A PROCEDURE ShowMenu()
B RETURN "1. Play"
C OUTPUT "2. Quit"
D ENDPROCEDURE
Question 3
A FUNCTION Area(S) RETURNS REAL
B RETURN S * S
C ENDFUNCTION
D CALL Area(5)
E OUTPUT Total
Question 4
A FUNCTION Perim(S) RETURNS REAL
B OUTPUT S * 4
C ENDFUNCTION
D OUTPUT Perim(3)
Question 5
A PROCEDURE ShowMenu()
B OUTPUT "1. Play"
C ENDPROCEDURE
D ShowMenu()

Answer Key

Choosing a procedure or a function for a task
Section A: Quick Questions
Q1
B — A function, because it produces one value that the program needs
Q2
A — A procedure, because it only does something
Q3
A — A function uses RETURN to send a value back to the line that called it
Q4
A — Calculating the area of a circle from its radius
Q5
B — Using OUTPUT to show a list of menu options
Section B: Match the Pairs
Q1
FUNCTION CalcArea(L : INTEGER, W : INTEGER) RETURNS INTEGER → Use a function because the task produces one value, the area, that the program needs.
Q2
PROCEDURE ShowMenu() → Use a procedure because the task only does something, such as displaying a menu, and gives no value back.
Q3
A function returns a value to the program, so the result can be stored in a variable. → Area ← CalcArea(5, 3)
Q4
A procedure only carries out actions such as OUTPUT, so it is run with its own CALL statement. → CALL ShowMenu()
Q5
PROCEDURE CalcArea(L, W) RETURN L * W ENDPROCEDURE → A procedure cannot return a value. The task produces one value the program needs, so it should be a FUNCTION with RETURNS and ENDFUNCTION.
Q6
FUNCTION ShowMenu() OUTPUT "1. Play" ENDFUNCTION → Displaying a menu only does something and returns no value, so it should be a PROCEDURE and not a FUNCTION.
Section C: Definitions
Q1
FUNCTION — Use a FUNCTION when a task produces one value the program needs, such as an area; use a PROCEDURE when it only does something, such as OUTPUT a menu.
Section D: Fill in the Blanks
Q1
PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE
Q2
FUNCTION Area(L : INTEGER) RETURNS INTEGER RETURN L * L ENDFUNCTION
Q3
FUNCTION Area(L, W) RETURNS INTEGER RETURN L * W ENDFUNCTION
Q4
CALL ShowMenu() area ← Area(4, 5)
Q5
area ← Area(3, 6) OUTPUT area
Section E: Spot the Error
Q1
C — Area produces one value the program needs, so it is a FUNCTION. A FUNCTION must be closed with ENDFUNCTION, not ENDPROCEDURE.
Q2
B — Showing a menu only does something, so it is a PROCEDURE. A procedure displays with OUTPUT and does not RETURN a value. The line should be OUTPUT "1. Play".
Q3
D — A function returns a value, so the call must be used in an expression or stored in a variable. CALL on its own throws the area away. It should be Total ← Area(5).
Q4
B — A function must RETURN its value so the program can use it. OUTPUT only displays it and gives nothing back. The line should be RETURN S * 4.
Q5
D — A procedure produces no value, so it is run with a CALL statement. Writing just the name is not a valid call. It should be CALL ShowMenu().