Skip to content

Computer Science · Ch 7 — Functions

The Advantages of Function

7.2.1

The Advantages of Function

Functions pay off the moment requirements change or grow — existing code can be reused instead of rewritten.

Reuse in action: the tent company again

Suppose the company later decides to design a new type of tent whose base is rectangular while the upper (conical) part stays the same. The programmer does not need to write the cone calculation again: the existing function con(l, r) can simply be called from the new program.

Similarly, if the company branches out into other products or services on which the same 18% tax applies, the function post_tax_price(cost) can be used directly, unchanged, wherever a post-tax amount is needed.

The advantages of using functions

Writing a program with functions gives four concrete benefits:

  • Readability increases. Especially in longer code, a program built from named functions is better organised, so it is easier to understand what each part does.
  • Code length reduces. The same code no longer has to be written at multiple places in the program — it is written once and called wherever needed. Shorter code with a single copy of each computation also makes debugging easier: a fix made inside the function fixes every use of it. …