Courses / Mathematics I
Programming Elements

Functions and Modularity

Mathematics I 187 words Free to read

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').

ElementRole
ParametersNamed inputs received when called
BodyThe algorithmic steps performed
Return valueThe 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.

ConceptMeaning
AbstractionTreating a function as a reliable black box
InterfaceThe known inputs and outputs
Common pitfall: Expecting a local variable to be visible outside its function. Functions communicate strictly through parameters and return values.
Two functions, one shared name, and no interference between them

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Programming Elements