Unit 5: Functions and Modularity
Until now, you’ve written code from top to bottom — one big block. Functions let you organise code into named, reusable pieces. This is how professional programmers write real software.
Lessons in This Unit
| Lesson | Title | Key Concepts |
|---|---|---|
| Lesson 14 | Writing Your Own Functions | def, function body, calling functions, DRY principle |
| Lesson 15 | Parameters and Return Values | parameters, arguments, return, None |
| Lesson 16 | Servos and Variable Scope | Servo motor, PWM, local vs global scope, global keyword |
New Hardware: 9G Servo Motor
A servo motor rotates to a precise angle (0°–180°) and holds it. Controlled with PWM signals. Used in RC vehicles, robotic arms, and camera stabilisers.
What You’ll Be Able to Do by the End
- Define and call your own functions
- Pass data into functions using parameters
- Get results back from functions using return values
- Understand where variables are visible (scope)
- Control a servo motor with a clean function-based interface
Functions are one of the most important concepts in all of programming. After this unit, your programs will be dramatically cleaner, more readable, and easier to fix and extend.