CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2006
    Posts
    49

    Talking Default function parameter

    Hi All,

    I have some question with default function parameter, The question is when I declare function pnd set parameter like

    void Func(int a, int b=1)
    {

    }

    void main()
    {
    //If I call
    Func(10);
    //The call
    Func(10,11)
    }

    In this case, what is function signature of "Func"?

    thank

  2. #2
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Default function parameter

    In the first call, 'a' will be 10 and 'b' will be the default 1.
    In the second call, 'a' will be 10 and 'b' will be 11
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Jul 2006
    Posts
    49

    Re: Default function parameter

    Hi Etherous,
    Thank you very much for your reply
    How the compiler generate function signature format?
    Thank

  4. #4
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: Default function parameter

    void (*Func)(int, int = 1)

  5. #5
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Default function parameter

    The function signature does not include the default values for the parameter. It is only a guide for the compiler to put some value if the parameter is not specified. Your function signature would be
    Code:
    void Func(int a, int b)
    The default parameter is visible only to the scope where you declared it. If you declared it in the header file, it can be seen by other modules using the header file. If you declared it in the CPP file, only those codes within that CPP can see the default values. You can even change the parameter defaults in other modules. For example you have another module named b.cpp. You can use the function as
    Code:
    extern void Func(int a = 10, int b = 0);
    
    ...
    
    Func();            // call it with 2 default parameters
    Hope it will clarify your concers

  6. #6
    Join Date
    Jul 2006
    Posts
    49

    Re: Default function parameter

    Hi rxbagain
    Thank you very much, This reply is very useful.
    thank!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured