Default function argument values


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Default function argument values

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    Default function argument values

    I've always put default function argument values in the header file. I see some code where the programmer did not specify a default value in the header, but did specify it in the body. Should the compiler (g++) have allowed this?
    When other compilation units include this header (with no default arguments defined), can the compiler know about the default arguments in the body of the other compilation unit? (I wouldn't think so.)

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    I always have it in both places so its very clear to the reader no matter where one looks. However, this is not "proper" really, its belongs only in the header. I do not know that its illegal or not to have it in the other place, but thats where it belongs.

    Supposedly its an error to have it in both places, due to a redefination problem, but I do not know if thats a compiler problem or if allowing it in both places is a compiler playing nice when it should not.

    The best result I found with some google action was that it can legally go in either place and that is fine, going in both should create an error (but may not on some compilers?), and it should go in the header as that is where the user of the function will look for it by convention. I do not know if this is true or not, but a few people said similar.

  3. #3
    Join Date
    Dec 2007
    Posts
    401
    > I see some code where the programmer did not specify a default value
    > in the header, but did specify it in the body.
    > Should the compiler (g++) have allowed this?

    The compiler should certainly allow it; it does not violate any C++ rule. (ODR is not violated as the default value is defined just once). The type of the function does not include default values for arguments - the type is the same; it is just that the compiler can generate the code to evaluate and provide these values if they are not specified by the programmer at the point of call.

    Even this is valid:

    Code:
    void foo( int, int, int=7 ) ;
    
    int main()
    {
        foo( 55, 666 ) ; // compiler generates foo( 55, 666, 7 ) ;
        void foo( int=1, int=2, int=3 ) ;
        foo( 55, 666 ) ; // compiler generates foo( 55, 666, 3 ) ;
        foo() ; // compiler generates foo( 1, 2, 3 ) ;
    }
    
    void foo( int, int=22, int ) {}
    This would not be good style, though.

    >When other compilation units include this header (with no default
    > arguments defined), can the compiler know about the default
    > arguments in the body of the other compilation unit?

    No. As far as the compiler is concerned, there are no default values for these arguments in this particular translation unit.

  4. #4
    Join Date
    Oct 2007
    Posts
    369
    Can you elaborate on this: "No. As far as the compiler is concerned, there are no default values for these arguments in this particular translation unit. "?

    Example. My main has this:
    Code:
    #include <iostream>
    using namespace std;
    #include "test.h"
    
    
    int main()
    {
        cout < "This is a test" << function() << endl;
        return 1;
    }
    test.h has this:

    Code:
    #ifndef TEST_H_
    #define TEST_H_
    
    int function(int a);
    
    #endif /* TEST_H_ */
    And test.cpp has this:

    Code:
    int function(int a=3)
    {
        return a;
    }
    This, I think correctly generates a compile error because when main is compiled, the compiler knows nothing about the default parameters in test.cpp.

    Basically, I think we've answered my question with the obvious: default parameters specified in one compilation unit has no effect on another compilation unit because the compiler doesn't see them.

  5. #5
    Join Date
    Dec 2007
    Posts
    401
    > default parameters specified in one compilation unit has no effect
    > on another compilation unit because the compiler doesn't see them.

    Yup. Actually we can make it stronger - default parameters that are not visible will not be evaluated by the compiler. For example,

    Code:
    void foo( int, int = 5 ) ;
    
    int main()
    {
         foo(22) ; // fine, calls foo( 22, 5 ) ; void foo( int, int = 5 ) is visible
         void foo( int, int ) ; // hides void foo( int, int = 5 ) 
         foo(22) ; // error; there is no default value for parameter 2.
    }

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    That said, there are still restrictions wrt default argument values. Here's what the standard says:
    For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, all parameters subsequent to a parameter with a default argument shall have default arguments supplied in this or previous declarations. A default argument shall not be redefined by a later declaration (not even to the same value). [Example:
    Code:
        void f(int, int);
        void f(int, int = 7);
        void h()
        {
            f(3);                       //  OK, calls  f(3,   7)
            void f(int = 1, int);       //  error: does not use default
        				//  from surrounding scope
        }
    
        void m()
        {
            void f(int, int);           //  has no defaults
            f(4);                       //  error: wrong number of arguments
            void f(int, int = 5);       //  OK
            f(4);                       //  OK, calls  f(4,   5);
            void f(int, int = 5);       //  error: cannot redefine, even to
        				//  same value
        }
        void n()
        {
            f(6);                       //  OK, calls  f(6,   7)
    Danny Kalev

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 05:12 AM
  2. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  3. Changing Default Values at Runtime
    By lucbrunet in forum VB Classic
    Replies: 5
    Last Post: 04-21-2005, 05:02 PM
  4. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 11:50 PM
  5. Multi-row calculations
    By Bob Hines in forum Database
    Replies: 7
    Last Post: 04-27-2000, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center