Default function arguments : Function Arguments « Function « C++






Default function arguments

  
#include <iostream>
#include <string>

using namespace std;

int askNumber(int high, int low = 1);

int main()
{
    int number = askNumber(5);
    cout << "Thanks for entering: " << number << "\n\n";
    
    number = askNumber(10, 5);
    cout << "Thanks for entering: " << number << "\n\n";

    return 0;
}

int askNumber(int high, int low)
{
    int num;
    do
    {
        cout << "Please enter a number" << " (" << low << " - " << high << "): ";
        cin >> num;
    } while (num > high || num < low);

    return num;
}
  
    
  








Related examples in the same category

1.A example of default arguments.A example of default arguments.
2.Compute area of a rectangle using default arguments.Compute area of a rectangle using default arguments.
3.Use default argumentUse default argument