Computer engineeringLevel 3 · Innovator
Functions and decomposition
A function is a named, reusable piece of code that takes inputs as parameters and hands back a result with return. Decomposition means breaking a large problem into small functions that each do one clear job, such as reading a data file, converting units or computing an average, so each piece can be understood, tested and reused on its own. A good function has a name that says what it does, a docstring stating its contract, including units, and no hidden dependence on variables outside it. Returning a value is more useful than printing it, because the result can feed the next calculation. Functions can also be passed to other functions, so one root finder works for any equation.
Why it mattersWriting a robot's wheel-speed conversion once as a function means a single fix reaches every part of the code that uses it.