Subroutines and Functions
Modularizing code using subroutines (procedures) and functions.
About This Topic
Subroutines and functions help students modularize code by dividing programs into smaller, focused blocks. Procedures carry out tasks without returning values, such as displaying output or updating variables. Functions compute results and return them to the main program, like calculating an average. Year 10 learners justify these tools for better readability and easier maintenance, meeting GCSE Computing standards in programming fundamentals.
In the Art of Programming unit, students differentiate procedures from functions by their return values and design structures with multiple subroutines for separate tasks, such as input validation or data processing. This practice strengthens decomposition and abstraction, core computational thinking skills essential for software development.
Active learning suits this topic well because students write, call, and test code live in editors. They see instant feedback on how subroutines simplify debugging and reuse, while pair refactoring turns abstract benefits into concrete gains. Collaborative challenges build confidence in structuring complex programs.
Key Questions
- Justify the use of subroutines to improve code readability and maintainability.
- Differentiate between a procedure and a function based on their return values.
- Design a program structure using multiple subroutines to perform distinct tasks.
Learning Objectives
- Analyze program logic to identify opportunities for code modularization using subroutines and functions.
- Compare and contrast the behavior of procedures and functions, explaining the significance of return values.
- Design a program structure for a given problem, effectively decomposing it into multiple, well-defined subroutines.
- Evaluate the impact of using subroutines on code readability, maintainability, and reusability.
- Create Python code that implements both procedures and functions to solve a multi-step problem.
Before You Start
Why: Students need to understand how to store and manipulate data to effectively use return values from functions.
Why: Students must be familiar with how to structure code execution before they can understand how to call and manage subroutines.
Why: Understanding how programs receive input and display output is fundamental to grasping the purpose of procedures that perform these actions.
Key Vocabulary
| Subroutine | A block of code designed to perform a specific task. It can be called from other parts of the program without rewriting the code. |
| Procedure | A type of subroutine that performs a task but does not return a value to the calling code. It is used for actions or commands. |
| Function | A type of subroutine that performs a task and returns a value to the calling code. It is used for calculations or data retrieval. |
| Call | The act of executing a subroutine or function from another part of the program. This transfers control to the subroutine. |
| Return Value | The data that a function sends back to the part of the program that called it. Procedures do not have return values. |
Watch Out for These Misconceptions
Common MisconceptionProcedures and functions work the same way.
What to Teach Instead
Procedures execute tasks without returning values, while functions must return a result. Active tracing exercises in pairs help students follow code paths step-by-step, clarifying execution differences through visual flowcharts and test runs.
Common MisconceptionSubroutines make code slower or unnecessary for small programs.
What to Teach Instead
Modular code runs at the same speed and builds good habits early. Hands-on refactoring in small groups demonstrates faster debugging and easier changes, as edits stay local to one subroutine.
Common MisconceptionFunctions can only return numbers.
What to Teach Instead
Functions return any data type, like strings or lists. Collaborative coding challenges let students experiment with varied returns, reinforcing flexibility through immediate error feedback and peer examples.
Active Learning Ideas
See all activitiesPair Programming: Modular Menu System
Pairs create a text-based menu program. Write procedures for displaying options and handling user choices, plus a function to validate inputs. Test the full program with sample runs and swap code for peer review.
Small Groups: Refactor Challenge
Provide groups with linear 'spaghetti' code for a quiz game. Identify repeated tasks and convert them into procedures and functions. Groups present their modular versions, explaining improvements in readability.
Whole Class: Function Trace Relay
Project a program with multiple functions. Divide class into teams; each traces one function's execution path on paper. Teams share findings to reconstruct the full flow and fix errors.
Individual: Subroutine Design Blueprint
Students outline a program for sorting student scores using pseudocode. Define 4-5 subroutines or functions with parameters and returns. Share blueprints for class feedback before coding.
Real-World Connections
- Software developers at game studios like Rockstar Games use functions extensively to manage complex game logic, such as character movement, AI behavior, and rendering graphics. Each function handles a specific task, making the massive codebase manageable.
- Web developers building e-commerce sites like Amazon use subroutines to organize common tasks like validating user input, processing payments, or displaying product information. This modular approach ensures consistency and speeds up development.
- Data scientists analyzing large datasets for companies like DeepMind employ functions to perform specific calculations, such as data cleaning, statistical analysis, or model training. This allows them to reuse complex analytical steps across different projects.
Assessment Ideas
Provide students with a short Python code snippet that uses a procedure and a function. Ask them to identify which is which, explain why, and describe what the return value (if any) represents.
Present students with a simple problem (e.g., calculating the area of a rectangle and printing the result). Ask them to write down the pseudocode for two subroutines: one to calculate the area (a function) and one to display the result (a procedure).
Pose the question: 'Imagine you are building a program to manage a library. What are three distinct tasks you could create as separate subroutines, and would each be a procedure or a function? Justify your choices.'
Frequently Asked Questions
What is the key difference between procedures and functions?
Why use subroutines to improve code readability?
How can active learning help students master subroutines and functions?
How do you assess understanding of program design with subroutines?
More in The Art of Programming
Sequence: The Order of Execution
Understanding that instructions are executed in a specific order.
2 methodologies
Selection: Conditional Logic (If/Else)
Implementing 'if', 'else if', and 'else' statements to control program flow.
2 methodologies
Selection: Case Statements
Using case statements (or switch statements) for multi-way branching.
2 methodologies
Iteration: Fixed Loops (For)
Using 'for' loops to repeat a block of code a predetermined number of times.
2 methodologies
Iteration: Conditional Loops (While)
Using 'while' loops to repeat a block of code until a condition is met.
2 methodologies
Variables and Constants
Working with variables and constants to store and manipulate information.
2 methodologies