Using multiple default parameter values - C++ Function

C++ examples for Function:Function Parameter

Description

Using multiple default parameter values

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
using  std::string;

// The function prototype including defaults for parameters
void show_data(const int data[], int count = 1, const string& title = "Data Values",
                                                   int width = 10, int perLine = 5);

int main()//from w  w w  .  j av a  2  s . co m
{
  int samples[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

  int dataItem {-99};
  show_data(&dataItem);

  dataItem = 13;
  show_data(&dataItem, 1, "Unlucky for some!");

  show_data(samples, sizeof (samples)/sizeof (samples[0]));
  show_data(samples, sizeof (samples)/sizeof (samples[0]), "Samples");
  show_data(samples, sizeof (samples)/sizeof (samples[0]), "Samples", 6);
  show_data(samples, sizeof (samples)/sizeof (samples[0]), "Samples", 8, 4);
}

void show_data(const int data[], int count, const std::string& title, int width, int perLine)
{
  std::cout << title << std::endl;

  // Output the  data values
  for (int i {} ; i < count ; ++i)
  {
    std::cout << std::setw(width) << data[i];
    if ((i+1) % perLine == 0)
      std::cout << std::endl;
  }
  std::cout << std::endl;
}

Result


Related Tutorials