Reuse: write once, call many times

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

Reuse: write once, call many times

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program needs to draw a line of stars in five different places. What is the best way to do this?
A Write one Line procedure and CALL it five times
B Copy and paste the same loop five times
2. Which statement about a subroutine is true?
A It must be written again each time it is used
B It is written once and can be called from many places
3. Which pair of calls reuses the same procedure to print lines of different lengths?
A CALL Line(5) and CALL Line(8)
B CALL Line(5) and CALL Stars(8)
4. What changes between CALL Line(3) and CALL Line(10)?
A The value passed in, so the line is a different length
B The procedure code has to be rewritten
5. What is the effect of using one Line procedure instead of repeating the loop code everywhere?
A The program is longer
B The program is shorter

Match the Pairs

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

Left columnRight column
1CALL Line(5) AThe length 5 is fixed inside the procedure and there is no parameter, so every call draws the same line. The procedure cannot be reused for different lengths.
2PROCEDURE Line(Length : INTEGER) BCALL Line(4) CALL Line(4) CALL Line(4)
3Reuse with different parameters: the same procedure draws a short line and then a long line CThe same loop is copied out twice instead of being written once in a procedure and called twice. This wastes typing and makes copy errors more likely.
4A subroutine written once is called from many places, so the program is shorter than repeating the loop DDefines the reusable code once, with a parameter so each call can use a different value
5FOR i ← 1 TO 5 OUTPUT "*" NEXT i FOR i ← 1 TO 5 OUTPUT "*" NEXT i ECALL Line(3) CALL Line(10)
6PROCEDURE Line() FOR i ← 1 TO 5 OUTPUT "*" NEXT i ENDPROCEDURE FCalls the Line procedure with the value 5, reusing code that was written once instead of repeating a loop

Definitions

CALL Line(5)

Fill in the Blanks

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

1.
____ Line(5)
2.
CALL ____(5) CALL Line(8) CALL Line(3)
3.
Width ← 7 CALL Line(_____)
4.
PROCEDURE Line(Count : INTEGER) FOR Index ← 1 TO _____ OUTPUT "*"
5.
NEXT Index ____________ CALL Line(6)

Spot the Error

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

Question 1
A CALL Line(5)
B CALL Line(8)
C Line(3)
Question 2
A PROCEDURE Line(N : INTEGER)
B FOR i ← 1 TO 5
C OUTPUT "*"
D NEXT i
E ENDPROCEDURE
Question 3
A FUNCTION Line(N : INTEGER)
B FOR i ← 1 TO N
C OUTPUT "*"
D NEXT i
E ENDPROCEDURE
Question 4
A CALL Line(10)
B CALL Line("4")
C OUTPUT "Done"
Question 5
A OUTPUT "Menu"
B CALL Line(20)
C OUTPUT "1 Play"
D CALL Line 20

Answer Key

Reuse: write once, call many times
Section A: Quick Questions
Q1
A — Write one Line procedure and CALL it five times
Q2
B — It is written once and can be called from many places
Q3
A — CALL Line(5) and CALL Line(8)
Q4
A — The value passed in, so the line is a different length
Q5
B — The program is shorter
Section B: Match the Pairs
Q1
CALL Line(5) → Calls the Line procedure with the value 5, reusing code that was written once instead of repeating a loop
Q2
PROCEDURE Line(Length : INTEGER) → Defines the reusable code once, with a parameter so each call can use a different value
Q3
Reuse with different parameters: the same procedure draws a short line and then a long line → CALL Line(3) CALL Line(10)
Q4
A subroutine written once is called from many places, so the program is shorter than repeating the loop → CALL Line(4) CALL Line(4) CALL Line(4)
Q5
FOR i ← 1 TO 5 OUTPUT "*" NEXT i FOR i ← 1 TO 5 OUTPUT "*" NEXT i → The same loop is copied out twice instead of being written once in a procedure and called twice. This wastes typing and makes copy errors more likely.
Q6
PROCEDURE Line() FOR i ← 1 TO 5 OUTPUT "*" NEXT i ENDPROCEDURE → The length 5 is fixed inside the procedure and there is no parameter, so every call draws the same line. The procedure cannot be reused for different lengths.
Section C: Definitions
Q1
CALL Line(5) — Calls the Line procedure written once, so the same code is reused many times with different parameters instead of being repeated in the program.
Section D: Fill in the Blanks
Q1
CALL Line(5)
Q2
CALL Line(5) CALL Line(8) CALL Line(3)
Q3
Width ← 7 CALL Line(Width)
Q4
PROCEDURE Line(Count : INTEGER) FOR Index ← 1 TO Count OUTPUT "*"
Q5
NEXT Index ENDPROCEDURE CALL Line(6)
Section E: Spot the Error
Q1
C — A procedure is run with the CALL keyword. The line should be CALL Line(3), so the same Line procedure is reused again.
Q2
B — The loop ignores the parameter and always repeats 5 times, so reuse with different values would not work. It should be FOR i ← 1 TO N.
Q3
A — Line only outputs stars and returns no value, so it is a procedure. It must start with PROCEDURE to match ENDPROCEDURE.
Q4
B — The parameter N is an INTEGER, so the argument must be the number 4, not the string "4". It should be CALL Line(4).
Q5
D — The argument must be inside brackets when calling a procedure. It should be CALL Line(20), reusing the same procedure.