Choosing Between Default Values and Overloaded Functions

Listings 13.1 and 13.2 accomplish the same thing, but the overloaded functions in 13.1 are simpler to understand and more natural to use. Also, if a third variation is needed—perhaps the user wants to supply either the width or the height, but not both—it is easy to extend the overloaded functions. The default value, however, will quickly become too complex to use as new variations are added.

How do you decide whether to use function overloading or default values? Here's a rule of thumb:

Look to function overloading when

  • There is no reasonable default value.

  • You need different algorithms.

  • You need to support differing types in your parameter list.

Get Sams Teach Yourself C++ in 24 Hours, Third 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.