Beginner level · Module 4 of 5 · 10 guided hours
Writing Reusable Functions
Package logic into named, testable functions so programs are easier to read, reuse and fix.
Learning objectives
- Define functions with parameters and return values
- Explain local and global scope
- Break a task into a set of cooperating functions
Topics
- def, parameters and return
- default arguments
- scope
- docstrings
Lessons
-
Lesson 1 Defining Functions with Parameters and Return Values
Write functions that take inputs and return results instead of printing everything directly.
defparameter vs argumentreturn valuedocstring
-
Lesson 2 Scope and Default Arguments
Understand where variables exist and make functions flexible with default values.
local scopeglobal scopedefault argumentkeyword argument
-
Lesson 3 Breaking a Problem into Functions
Plan a program as a set of small functions, each with one clear job.
decompositionsingle responsibilitymain()if __name__ == '__main__'
Practical activity
Refactor an existing 60-line script provided by the instructor into well-named functions without changing its output, then explain the structure to a peer.
Expected outcomes
- Code organised into functions with clear names and docstrings
- Programs that are easier to test and extend
Assessment
Code review against a rubric: function size, naming, use of return values, docstrings.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.