Reusable Blocks
A function packages logic under a name so it can be reused instead of rewritten. This is modularity: building programs from small, independent parts following the DRY principle ('Don't Repeat Yourself').
| Element | Role |
|---|---|
| Parameters | Named inputs received when called |
| Body | The algorithmic steps performed |
| Return value | The output handed back to caller |
Calling f(3, 4) passes arguments to parameters, executing the logic once and invoking it many times with distinct inputs.
Scope & Encapsulation
Scope governs where names are visible. A local variable defined inside a function exists only there and vanishes upon return.
This encapsulation hides internal details behind a clean interface. Two functions can safely use a variable named i without interference.
| Concept | Meaning |
|---|---|
| Abstraction | Treating a function as a reliable black box |
| Interface | The known inputs and outputs |
Common pitfall: Expecting a local variable to be visible outside its function. Functions communicate strictly through parameters and return values.