Default arguments can be useful in several scenarios:
For example, consider a function that draws a shape:
void DrawShape(std::string name, int width,
int height, int x = 0, int y = 0) {
// Draw the shape...
}
By providing default values for x
and y
, we make them optional for the caller. Callers can use the defaults for a simple case:
// Draws at (0, 0)
DrawShape("Square", 10, 10);
Or they can provide custom values when needed:
// Draws at (100, 100)
DrawShape("Circle", 15, 15, 100, 100);
Answers to questions are automatically generated and may not have been reviewed.
Learn the basics of writing and using functions in C++, including syntax, parameters, return types, and scope rules.