Programming Essentials · BE5B33PRG · Topic 2 of 9
Functions and scope
A function names reusable behavior and defines an interface.
Key ideas
Define and call
def double(x): return 2 * x defines a function; double(3) returns 6. A definition alone does not run its body.
Return versus print
return gives a value to the caller; print only displays it. A function without an explicit return gives None.
Local names
Parameters and assigned names are local by default. Distinguish a new local binding from mutation of an object passed in.
Contract
Write a docstring, specify inputs and output, and test a normal and boundary case.
In one line: A useful function has a clear input, result and reason to exist.
Sources
Written by Ucenly. Based on the BE5B33PRG course materials:
- lec03-functions-mn-2025_16_9, p. 9, 10, 12, 14