# Default Parameters.

Within C you could not provide default values for function parameters. Well, C++ has come to your rescue.

        #include <iostream.h>

        void Func( int one, int two=2, int three=3);

        main ()
        {
            Func(10, 20, 30);
            Func(10, 20);   // Let the last parm default
            Func(10);       // Just provide the required parm.
        }

        void Func( int one, int two, int three)
        {
            cout << "One   = " << one   << endl;
            cout << "Two   = " << two   << endl;
            cout << "Three = " << three << endl << endl;
        }

From this example, you can see that the prototype/function declaration gives default values for the second and third parameters. It is now down to the programmer calling the function to decide how many s/he wants to provide.

There are some basic rules that should be applied when using default parameter values.

- When a default is provided, all remaining parameters must be given defaults. There can not be any holes.
- Default values must be of the correct type.
- Defaults can be placed on the function prototype or the function definition. They can also be placed in both locations at the same time. I have been told its good practice to place them in the prototype only.

## Examples:

## See Also:

Function Name Overloading.

## C References

Function basics.

Top | Master Index | Keywords | Functions