Recursion

Recursion is when a function calls itself. This seems like a recipe for disaster, but recursion is a powerful way to express some operations that are naturally defined in terms of themselves.

Another Binary Search

Consider the binary search introduced in Chapter 3, “Arrays and Algorithms”: You look at the midpoint of the range, and if that value equals the key, then you have succeeded. If the key is less than the middle value, you search the first half, and if the key is greater than the value, you search the second half. Finally, if the range is empty, you have failed. Note that this description of a search is defined in terms of searching! Here is a version of bsearch() that has no explicit loop. Instead, it calls itself to search ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.