Subroutine Studio
Subroutines you can call by name: procedures that carry out a job, functions that hand a value back, the parameters passed in, local variables and scope, and why structured code is easier to reuse, test and read.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
Code you can call by name
Instead of writing the same steps again and again, you can wrap them in a named block and call it whenever you need it. That block is a subroutine. Some subroutines just do a job; others work something out and hand the answer back. This module works through procedures, functions, parameters, return values, scope and the reasons structured code is worth it.
Subroutine words to know
Learn these before you start calling code.
Two kinds of subroutine
The split is about what happens at the end of the block.
Say why it earns its place
In the exam, do not stop at defining a subroutine; explain why it earns its place. The usual four reasons are reuse, simpler testing, clearer reading and easier teamwork. Then split the two kinds by what happens at the end: one hands something back for you to keep, the other just carries out its job. And treat any name born inside a block as trapped there.
Match the term
- subroutine
- parameter
- return value
- local variable
- a block of code you call by name
- a value passed in on the call
- the answer a function hands back
- a variable that lives only inside its subroutine
Match the example
- a block named show_menu that prints choices
- a block named square that gives back a number
- the number you send into square
- the answer square gives you
- this is a procedure
- this is a function
- this is a parameter
- this is a return value
What makes it a function?
A function does one thing that a procedure does not. What is it?
- It hands a value back to the caller
- It has a name
- It can take parameters
- It can be called
True about subroutines
Select the TWO true statements about subroutines.
- They let you reuse code without rewriting it
- A parameter passes a value into a subroutine
- A local variable can be read anywhere in the program
- Every subroutine must hand a value back
Count the calls
A function is called 4 times while the program sets up and 3 times inside the main loop. Add the two counts to find how many times the function runs in total. What is the answer?
Order a function call
Put the steps of calling a function in order, earliest first.
- The function is called with a value
- The parameter receives that value
- The steps inside the function run
- A value is handed back to the caller
Complete the rules
A _____ is a block of code with its own name. A value you pass in when you call it is a _____. A function hands a value back to the caller, and this is its _____ value. A variable that exists only inside a subroutine is _____.
Spot the benefits
Tap the TWO statements that describe why subroutines make code easier to work with.
- the same block can be reused instead of copied
- one block can be tested on its own
- the code must be copied wherever it is needed
- every variable can be seen from everywhere
Name the part
A function named average is at work. Read each line and name the part it describes.
- The function average is called and the numbers 4 and 6 are sent in to it. What are the 4 and 6?
- The function works out 5 and hands it back to the caller. What is that 5?
- Inside average a variable named total is made and used only in that block. What is total?
Explain subroutines
Explain what subroutines are and why programmers use them.
- Say what a parameter and a return value each are
- Explain the difference between a procedure and a function
- Give two reasons structured code with subroutines is easier to work with