liveBook · Manning

Function

As explained in How Computers Make Books

A function is a named computational unit that accepts parameters, performs operations, and can return a result.

A function is a named computational unit: it accepts parameters, performs operations, and may return a result. Naming a calculation lets a larger program reuse it as a small building block. section 7.0 section 7.3

A power function

A power function accepts two inputs, x and y, and calculates xᵞ. It handles the base case y = 0 by returning 1; otherwise, it calculates x multiplied by xᵞ⁻¹. Reapplying the function reduces 2⁵ to 2 × 2⁴, then to 2 × 2 × 2³, until the result is 32. section 7.5

Functions can organize an algorithm by separating its jobs:

  • Define parameters that receive the inputs needed for the calculation. section 4.2
  • Perform operations and use conditions to choose a path. section 7.3
  • Return information to the calling function, such as true or false, instead of printing it directly. section 4.2

How How Computers Make Books covers it

How deep it goes
Defines parameters, operations, conditions, and return values, then demonstrates recursive evaluation without presenting language-specific implementation details.
Examples
Uses a recursive power calculation and a text-search algorithm whose compare function returns true or false.
What you can do after
Build it from scratch by defining reusable functions with parameters, conditional logic, recursive calls, and returned results.
How it connects
Connects functions with arithmetic, conditional expressions, recursion, Boolean values, and structured data such as lists.

Also explained in

Compare how 20 books explain function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Computers Make Books. The section links point to the parts of the book it draws on.