Subroutine Studio
Why write the same code twice? Meet subroutines: procedures and functions, the parameters you pass in, the values they return, and where variables can be seen.
Revise this, the fun way
Play it interactively, earn XP and build a streak, free.
Start revising freeWhat you'll cover
Write it once 🔀
Imagine writing the same ten lines of code every time you need to add up a bill. Tiring! A **subroutine** (also called a subprogram) is a named block of code you write once and CALL whenever you need it. Subroutines make programs shorter, easier to test and easier to read.
Words to know 🔑
Four words run through this topic:
What does a function do? 🎯
What makes a subroutine a function?
- It returns a value to the code that called it
- It never takes any input
- It can only be called once
- It deletes all global variables
Procedure or function? 🆚
Both are subroutines. The difference is whether a value comes back.
Match the term 🔗
- Subroutine
- Parameter
- Local variable
- Library subroutine
- A named block of code you can call
- Data passed into a subroutine when it is called
- A variable that only exists inside its subroutine
- A ready-made subroutine that comes with the language
Global vs local 🌐
Scope means where a variable can be seen. A LOCAL variable exists only inside its own subroutine and disappears when the subroutine ends. A GLOBAL variable can be seen anywhere in the program. A common bug is trying to use a local variable outside its subroutine, where it does not exist.
Why use subroutines? ✅
Which THREE are good reasons to use subroutines?
- You can reuse the same code in many places
- You can test each part on its own
- The program is easier to read
- They always make the program run slower
- They remove the need to plan the program
Trace the function 📊
An interactive activity.
Which is it? 🔎
A subroutine prints a welcome message on the screen but sends no value back. What is it?
- A procedure
- A function
- A parameter
- A global variable
Build a subroutine 🪜
An interactive activity.
Complete the summary 🧩
A _____ is a named block of code you can call again and again. A function returns a _____, but a procedure does _____ return one. Data passed into a subroutine is called a _____. A variable that exists only inside a subroutine is called a _____ variable.
True or false? 🖍️
An interactive activity.
Spot the subroutine 🧭
An interactive activity.
Why subroutines? ✍️
An interactive activity.