FAQ 2.04 What are the basics of default parameters?

graphics/new_icon.gif

C++ allows functions to have default parameters. This is useful when a parameter should have a specified value when the caller does not supply a value. For example, suppose that the value 42 should be passed to the function f() when the caller does not supply a value. In that case, it would make the calling code somewhat simpler if this parameter value were supplied as a default value:

 void f(int x=42);                                    <-- 1 void f(int x)                                        <-- 2 {   //... } int main() {   f(29);                                             <-- 3   f();                                               <-- 4 } 

(1) Declare the default parameter(s) in the function declaration

(2) Don't repeat the default parameter(s) in the function definition

(3) Passes 29 to f()

(4) Passes 42 to f()



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

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