Parameters with Default Values

This is a new language facility (not available in C) whose goal is a further improvement in code readability and modifiability. When declaring a function, you can specify default values for one or more parameters in the parameter list.

Here is a declaration for a function sum() that computes the sum of given number of components of an array of double values. This declaration uses the initialization syntax for the second formal parameter to specify the default value 25.

double sum (const double a[], int n=25);          // a prototype

This initialization syntax directs the compiler to use the default value specified in the prototype when no actual value is specified in the call in the client code.

 double total; double ...

Get Core C++ A Software Engineering Approach 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.