PASSING DEFAULT ARGUMENTS


Visual C++ also provides you with the ability to set up functions that can process default arguments. There are a few rules you need to be aware of when setting up a function to accept default arguments. First of all, you must assign a constant value to all default arguments. In other words, you cannot use a variable or other type of value. Finally, any argument that follows a default argument must be another default argument.

The following example demonstrates how to set up a function with a default argument:

 private:Void ShowAge( Int32 intAge = 32 ) {   MessageBox::Show( intAge.ToString() ); } 

You can call the function in the previous example without passing it an argument, as shown here:

 ShowAge(); 

Alternatively, you can call the function and pass it an argument, like this:

 ShowAge( 100 ) 

Default arguments are useful because they allow you to ensure that a function's parameters are initialized. They can be useful in speeding application development by allowing you to use abbreviated function calls for the most common cases and detailed versions when you need to specify arguments outside of the norm.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net