previous | start

Default Arguments

Functions can specify default argument values in the function declaration.

If a default argument value is given in a function declaration, the caller can override the default value by passing an argument.

But if the caller omits the argument altogether, the default argument value is used.

double area(double radius = 1.0 );

int main()
{
  double a1, a2;
  double r;

  cin >> r;

  a1 = area(r);  // radius = r
  a2 = area();   // radius = 1.0

}
double area(double radius) 
{
  return radius * radius * PI;
}


previous | start