CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Sep 2007
    Posts
    85

    function whith default parameters

    hi mans!!..

    I have a problem, and "googling" ddidn't find nothing..
    myu problem is that i have a function such

    Code:
    codeguru(int i, int y, int z)
    {
         cout << i, y, z;
    }
    now the function is right!!..
    but i don't wont to pass the y variable,
    so i can do :

    Code:
    codeguru(int i, int y=3 , int z)
    {
         cout << i, y, z;
    }
    but when i call it, whith

    Code:
    codeguru(2, ,4);
    isn't write..
    why is wrong??..
    how can i make it better?

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: function whith default parameters

    The default parameters need to be the right-most parameters in the function.

    Code:
    void codeguru(int i, int z, int y=3)
    {...}
    Laitinen

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: function whith default parameters

    You can only provide default parameters from right to left, you cannot omit the center ones. When calling a function with default parameters you simply omit the parameters you don´t want to specify, you cannot use placeholders. In your example you have to specify both y and z, because z is the rightmost parameter and it has no default value, so you must specify z. Once you specify z you must specify y, too, because y is left of z.
    You can reorder your parameters and move the ones which will most likely use default values to the right.
    - Guido

  4. #4
    Join Date
    Sep 2007
    Posts
    85

    Re: function whith default parameters

    i need to assign a temporaney variable, becouse when i use this function
    i'm free to ommit the variable...

    i use how you told me..

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    void function(int x, int y=3) 
    {
            cout << x << endl << y;  
    }
    int main(int argc, char *argv[])
    {
        function(2,);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    but there are errors in this line:

    Code:
     function(2,);
    this is an error becouse i don't pass the value of variable,
    but in function i asssign it!!...
    if i don't wont to assign when i call, how i can do??..

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: function whith default parameters

    The function call should be: function(2);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: function whith default parameters

    Quote Originally Posted by Rooting
    Code:
    cout << i, y, z;
    It's entirely possible that that line does not do what you think it does.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Sep 2007
    Posts
    85

    Re: function whith default parameters

    Quote Originally Posted by Graham
    It's entirely possible that that line does not do what you think it does.
    i try not this source but this one:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    void function(int x, int y=3) 
    {
            cout << x << endl << y;  
    }
    int main(int argc, char *argv[])
    {
        function(2,);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    in this source is right!!..
    but is wrong when i call the function..

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: function whith default parameters

    Quote Originally Posted by laserlight
    The function call should be: function(2);
    Repeated.

  9. #9
    Join Date
    Sep 2007
    Posts
    85

    Re: function whith default parameters

    mmm...ok..
    when i have 2 variable now is ok..
    but, in my case i have 3 variable as:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    void function(int x=1, int y=3, int z = 4) 
    {
            cout << x << endl << y << endl << z;  
    }
    int main(int argc, char *argv[])
    {
        function(,5,);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    in this case i have 3 variable, and i wold like to specify the y one..
    the y is in the midle..
    the expression

    function(,5,) is wrong!.
    how can i resolve??..

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: function whith default parameters

    C++ doesn't support that. It can only supply default values to those parameters on the right of the ones you specify.

    And you need to stop trying to use commas that don't separate anything. As far as I know that's always a syntax error.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: function whith default parameters

    And you need to stop trying to use commas that don't separate anything. As far as I know that's always a syntax error.
    There is at least one exception where it is allowed, namely, when it comes after an initialiser list, e.g., {1, 2, 3,}. That does not apply here, though.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: function whith default parameters

    Why not just overload the functions?
    Code:
    void func(int x, int y, int z)
    {
    	cout << x << y << z;
    }
    
    void func(int y)
    {
    	func(1, y, 2);
    }
    int main()
    {
    	func(3);
    }
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #13
    Join Date
    Sep 2007
    Posts
    85

    Re: function whith default parameters

    mmm...
    i'm making a library, so I don't know if the user can use the x or y or z variable...
    understand me??..
    so if the user don't specify the x variable, but put the y and z , the library must go!!..
    it's the function of class..
    overloading as graham sad is very simple, but not elegant, becouse i have 15 function in
    this class..and i don't know if he specify the x or y or z one..
    any suggestion??

  14. #14
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: function whith default parameters

    Perahps variadic function might help.. Do google to find out the implementation of variadic function...
    Dont forget to rate my post if you find it useful.

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: function whith default parameters

    I think that neither function overloading nor variadic functions will cut it here. What is needed is named arguments, which is not a feature of C++. The reason is that there is nothing to differentiate foo(x, y) from a call to foo(x, z) or foo(y, z).

    I am not sure if this really is a good idea, but I would propose a workaround with std::map and some function templates:
    Code:
    #include <iostream>
    #include <map>
    
    template<char c1>
    std::map<char, int> make_values(int v1)
    {
        std::map<char, int> ret;
        ret[c1] = v1;
        return ret;
    }
    
    template<char c1, char c2>
    std::map<char, int> make_values(int v1, int v2)
    {
        std::map<char, int> ret;
        ret[c1] = v1;
        ret[c2] = v2;
        return ret;
    }
    
    template<char c1, char c2, char c3>
    std::map<char, int> make_values(int v1, int v2, int v3)
    {
        std::map<char, int> ret;
        ret[c1] = v1;
        ret[c2] = v2;
        ret[c3] = v3;
        return ret;
    }
    
    void foo(const std::map<char, int>& values)
    {
        std::map<char, int>::const_iterator iter = values.find('x');
        int x = (iter == values.end()) ? 0 : iter->second;
        iter = values.find('y');
        int y = (iter == values.end()) ? 0 : iter->second;
        iter = values.find('z');
        int z = (iter == values.end()) ? 0 : iter->second;
    
        std::cout << x << ' ' << y << ' ' << z << std::endl;
    }
    
    int main()
    {
        foo(make_values<'x'>(1));
        foo(make_values<'z', 'y'>(10, 20));
        foo(make_values<'y', 'x', 'z'>(2, 3, 4));
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

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