------------------------------------------------------------------------------
MC logo
Default Parameters
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Functional Average] [Functional Average II] [Functional Average III] [Default Parameters] [Function Overloading] [Reference Parameters] [Constant References] [Namespaces] [Namespaces I] [Using Namespaces]
def2.cc
#include <iostream>

using namespace std;

// Print a row with a starting and ending character, with a specified number
// of middle characters between them.
void row(char ends, int num_middle, char middle) 
{
        cout << ends;
        for(int m = num_middle; m--;) cout << middle;
        cout << ends << endl;
}

// Draw a box with text.
void box(int width = 9, int height = 5, bool fill = false)
{
        // Top row
        row('+', width-2, '-');

        // Draw internal rows.
        for(int m = height-2; m--;) 
                row('|', width-2, fill ? '#' : ' ');

        // Final row.
        row('+', width-2, '-');
}

// Box driver.
int main()
{
        box();
        cout << endl;
        box(4);
        cout << endl;
        box(15, 7);
        cout << endl;
        box(3,3,true);
}

Default values may be given to parameteters. These are used if the parameter is not sent. The rules require: So these are illegal:

void joe(int x = 5, double d) { ... }
fred(10, , 20);

Defaults are C++ only.