Default Values for Parameters

To make a function as general as possible, you need to specify all its parameters. It is often a bad idea to make a function's result depend on some global parameter, and it is frustrating to use a function that depends on some mysterious constants.

Specifying too many parameters as arguments is clumsy and makes the function's use hard to remember. In Chapter 5, “Structures and Pointers,” you saw one solution—to wrap up the parameters as members of a structure, which could then be efficiently passed by reference to a function. This solution is the equivalent of the named parameters that some languages support. Here is an example of what this kind of parameter passing looks like. do_operation() is a function with ...

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.