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.


Examples:


See Also:

o Function Name Overloading.

C References

o Function basics.



Martin Leslie

AltStyle によって変換されたページ (->オリジナル) /