Thread: Why default argument for the last parameter is mandatory ?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    59

    Exclamation Why default argument for the last parameter is mandatory ?

    Dear Friends,

    I faced a compilation error in the following code :
    Code:
    #include <iostream>
    
    using namespace std;
    
    void addition(int a, int b = 2, int c);
    
    int main()
    {   
        int a = 10, b = 20, c = 30;
        addition(10,20,30);
        return 0;
    }
    
    void addition(int a, int b, int c)
    {  
       cout << "\n Sum  = "<< a + b + c;
       return;
    }
    Error : default argument missing for parameter 3 of ‘void addition(int, int, int)’

    My question is that when i have called addition() with the 3rd argument, then what is the necessity of having the default argument for the 3rd parameter ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The default argument for the third parameter is necessary because you have a default argument for the second parameter. Once you have one default parameter the rest of the parameters to the right of that one must also have default parameters as well.


    Jim

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because that's the language rules. Default values, if provided must be for rightmost arguments. That means, if there is only one argument with a default value, it must be the last one. If there are two arguments with default values, they must be the last two. And so on.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    Thanks friends, surprisingly though, i could not find this important point in any text so far. Thanks again for the timely replies.. may i ask why such rule is there ? any specific reason or it's just a rule ??

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Eliminates ambiguity in some circumstances.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if you have the 2nd parameter as default, then how does the compiler know what value to put for the last? Consider:

    addition(0, 1, 2); // Fine
    addition(0, 2); // Wait: does "2" refer to the 2nd or 3rd parameter?

    Technically, while a parameter is optional, you can still provide an argument for it. So how does the compiler know you skip it or not?
    It would have to be made explicit somehow, like:

    addition(0, [skip], 2);
    or
    addition(0, [c]=2);

    Where I simply put "[skip]" and "[c]" as some placeholds for some syntax. Such syntax does not exist in the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by techie_san778 View Post
    Thanks friends, surprisingly though, i could not find this important point in any text so far. Thanks again for the timely replies.. may i ask why such rule is there ? any specific reason or it's just a rule ??
    Are you kidding? It is hopefully obvious why it must be this way...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default value for function parameter
    By Justin H in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2012, 01:08 AM
  2. Redefinition of default parameter
    By 843 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2011, 02:07 AM
  3. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 11:59 AM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM
  5. default parameter
    By laasunde in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2003, 10:55 PM