The very concept of a function is chameleon-like: it takes on different aspects when viewed in different contexts. The first—and simplest—perspective on functions we’re going to consider is that of a code reuse vehicle.
The idea of subroutines is a very old one, a practical concession to managing complexity. Without subroutines, programming would be a very repetitive affair indeed. Subroutines simply package some bit of repetitive functionality, give it a name, and allow you to execute that bit of functionality at any time simply by referring to that name.
Very often, a subroutine is used to package an algorithm, which is simply an understood recipe for performing a given task. Let’s consider the algorithm ...