Difference between a procedure and a function

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

Difference between a procedure and a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which one gives back a value that can be used where it is called?
A A function
B A procedure
2. Which header is a function?
A FUNCTION Total(a, b) RETURNS INTEGER
B PROCEDURE Total(a, b) RETURNS INTEGER
3. How do you run a procedure called DrawLine?
A total ← CALL DrawLine()
B CALL DrawLine()
4. Which statement uses a function correctly?
A total ← AddUp(4, 5)
B CALL AddUp(4, 5)
5. Which subroutine would you use to draw a line of 20 stars on the screen and return nothing?
A A function
B A procedure

Match the Pairs

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

Left columnRight column
1FUNCTION AddUp(X : INTEGER) RETURNS INTEGER ATotal is a function, and CALL is only for procedures. The returned value is lost. Write Answer ← Total(3, 4) or OUTPUT Total(3, 4).
2PROCEDURE DrawLine() BA function header: RETURNS gives the data type of the value the function sends back.
3A function's returned value is used where it is called, for example stored in a variable CDrawLine is a procedure and returns nothing, so there is no value to store. It should be run with CALL DrawLine().
4A procedure is called as its own statement using CALL DA procedure header with no RETURNS part, because a procedure gives nothing back.
5CALL Total(3, 4) ECALL DrawLine()
6Result ← DrawLine() FCost ← Total(3, 4)

Definitions

RETURNS

Fill in the Blanks

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

1.
FUNCTION Twice(N : INTEGER) _______ INTEGER
2.
____ DrawLine(5)
3.
FUNCTION Twice(N : INTEGER) RETURNS INTEGER ______ N * 2 ENDFUNCTION
4.
_________ DrawLine(Size : INTEGER) OUTPUT "-----" ENDPROCEDURE
5.
Answer _ Twice(4) OUTPUT Answer

Spot the Error

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

Question 1
A FUNCTION Sum() RETURNS INTEGER
B Result ← 5 + 3
C OUTPUT Result
D ENDFUNCTION
Question 2
A PROCEDURE DrawLine()
B OUTPUT "----------"
C ENDFUNCTION
D CALL DrawLine()
Question 3
A PROCEDURE Hello()
B OUTPUT "Hi"
C ENDPROCEDURE
D Hello()
Question 4
A FUNCTION Pi() RETURNS REAL
B RETURNS 3.14
C ENDFUNCTION
D OUTPUT Pi()
Question 5
A PROCEDURE Show(N : INTEGER)
B RETURN N * 2
C ENDPROCEDURE

Answer Key

Difference between a procedure and a function
Section A: Quick Questions
Q1
A — A function
Q2
A — FUNCTION Total(a, b) RETURNS INTEGER
Q3
B — CALL DrawLine()
Q4
A — total ← AddUp(4, 5)
Q5
B — A procedure
Section B: Match the Pairs
Q1
FUNCTION AddUp(X : INTEGER) RETURNS INTEGER → A function header: RETURNS gives the data type of the value the function sends back.
Q2
PROCEDURE DrawLine() → A procedure header with no RETURNS part, because a procedure gives nothing back.
Q3
A function's returned value is used where it is called, for example stored in a variable → Cost ← Total(3, 4)
Q4
A procedure is called as its own statement using CALL → CALL DrawLine()
Q5
CALL Total(3, 4) → Total is a function, and CALL is only for procedures. The returned value is lost. Write Answer ← Total(3, 4) or OUTPUT Total(3, 4).
Q6
Result ← DrawLine() → DrawLine is a procedure and returns nothing, so there is no value to store. It should be run with CALL DrawLine().
Section C: Definitions
Q1
RETURNS — Only a function has RETURNS: it gives back a value used where it is called. A procedure returns nothing and is run on its own with CALL.
Section D: Fill in the Blanks
Q1
FUNCTION Twice(N : INTEGER) RETURNS INTEGER
Q2
CALL DrawLine(5)
Q3
FUNCTION Twice(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q4
PROCEDURE DrawLine(Size : INTEGER) OUTPUT "-----" ENDPROCEDURE
Q5
Answer ← Twice(4) OUTPUT Answer
Section E: Spot the Error
Q1
C — A function must give its value back to the place it was called, so it needs RETURN Result. OUTPUT only displays the value and returns nothing.
Q2
C — A procedure starts with PROCEDURE, so it must finish with ENDPROCEDURE. ENDFUNCTION is only used to close a function.
Q3
D — A procedure is run as its own statement using CALL. The line should be CALL Hello().
Q4
B — RETURNS belongs in the function header to give the data type. Inside the function the keyword that sends a value back is RETURN 3.14.
Q5
B — A procedure returns nothing, so it cannot use RETURN. It should display the value with OUTPUT N * 2.