What a subroutine is

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

What a subroutine is

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes a subroutine best?
A A named block of code that runs when it is called
B A variable that stores a number for later
2. How many times must you write the code inside a subroutine?
A Again every time you want it to run
B Once, then call it whenever needed
3. What are the two kinds of subroutine in IGCSE pseudocode?
A Procedures and functions
B Loops and arrays
4. Why do programmers break a program into subroutines?
A It makes the program run without a computer
B It splits the program into smaller named parts
5. When does the code inside a subroutine run?
A When the subroutine is called
B As soon as it is defined

Match the Pairs

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

Left columnRight column
1PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE AFUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION
2CALL ShowMenu() BThe same statements are copied out each time instead of being written once in a subroutine and called.
3The two kinds of subroutine are procedures and functions, and a function gives back a value CCALL ShowMenu() CALL ShowMenu() CALL ShowMenu()
4A subroutine is defined once and can be called many times DA named block of statements that is defined once and does a job when it is called
5PROCEDURE ShowMenu() OUTPUT "1. Play" CALL ShowMenu() EThe procedure has no ENDPROCEDURE, so the block of statements is never closed and the subroutine is not properly defined.
6OUTPUT "1. Play" OUTPUT "1. Play" OUTPUT "1. Play" FRuns the subroutine named ShowMenu, then control returns to the next line

Definitions

subroutine

Fill in the Blanks

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

1.
_________ ShowMenu() OUTPUT "Welcome" ENDPROCEDURE
2.
____ ShowMenu() OUTPUT "Pick an option" CALL ShowMenu()
3.
FUNCTION Double(N : INTEGER) _______ INTEGER RETURN N * 2 ENDFUNCTION
4.
PROCEDURE DrawLine() OUTPUT "----------" ____________
5.
FUNCTION Five() RETURNS INTEGER ______ 5 ENDFUNCTION

Spot the Error

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

Question 1
A SUBROUTINE Countdown()
B OUTPUT "3 2 1 Go!"
C ENDPROCEDURE
D CALL Countdown()
Question 2
A PROCEDURE Welcome()
B OUTPUT Welcome!
C ENDPROCEDURE
D CALL Welcome()
E CALL Welcome()
Question 3
A FUNCTION GetPi() RETURNS REAL
B OUTPUT 3.14
C ENDFUNCTION
D OUTPUT GetPi()
Question 4
A PROCEDURE Line()
B OUTPUT "-----"
C ENDFUNCTION
D CALL Line()
E CALL Line()
Question 5
A PROCEDURE Hello()
B OUTPUT "Hello"
C ENDPROCEDURE
D Hello()
E CALL Hello()

Answer Key

What a subroutine is
Section A: Quick Questions
Q1
A — A named block of code that runs when it is called
Q2
B — Once, then call it whenever needed
Q3
A — Procedures and functions
Q4
B — It splits the program into smaller named parts
Q5
A — When the subroutine is called
Section B: Match the Pairs
Q1
PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE → A named block of statements that is defined once and does a job when it is called
Q2
CALL ShowMenu() → Runs the subroutine named ShowMenu, then control returns to the next line
Q3
The two kinds of subroutine are procedures and functions, and a function gives back a value → FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION
Q4
A subroutine is defined once and can be called many times → CALL ShowMenu() CALL ShowMenu() CALL ShowMenu()
Q5
PROCEDURE ShowMenu() OUTPUT "1. Play" CALL ShowMenu() → The procedure has no ENDPROCEDURE, so the block of statements is never closed and the subroutine is not properly defined.
Q6
OUTPUT "1. Play" OUTPUT "1. Play" OUTPUT "1. Play" → The same statements are copied out each time instead of being written once in a subroutine and called.
Section C: Definitions
Q1
subroutine — A named block of code, written once, that runs whenever it is called. Procedures and functions are the two kinds, and they break a program into parts.
Section D: Fill in the Blanks
Q1
PROCEDURE ShowMenu() OUTPUT "Welcome" ENDPROCEDURE
Q2
CALL ShowMenu() OUTPUT "Pick an option" CALL ShowMenu()
Q3
FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q4
PROCEDURE DrawLine() OUTPUT "----------" ENDPROCEDURE
Q5
FUNCTION Five() RETURNS INTEGER RETURN 5 ENDFUNCTION
Section E: Spot the Error
Q1
A — A subroutine is defined with the keyword PROCEDURE (or FUNCTION), not SUBROUTINE. The first line should be PROCEDURE Countdown().
Q2
B — Text to be displayed must be in quotation marks. The line should be OUTPUT "Welcome!". The procedure is written once and then called twice.
Q3
B — A function must send a value back to the place where it was called. It needs RETURN 3.14, not OUTPUT.
Q4
C — A procedure must finish with ENDPROCEDURE. ENDFUNCTION is only used to close a function.
Q5
D — A procedure is run using the keyword CALL. The line should be CALL Hello(), so the subroutine can be called as many times as needed.