Thread: default Arguments

  1. #1
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6

    Question default Arguments

    for the function having prototype....

    void fun(int = 10, int = 20, int = 30, int = 40);

    how to make sure that when it is called with two arguments
    such as.....

    fun(1, 2);

    these two are treated as the 1st & 3rd arguments
    while the 2nd & 4th take the default values???????????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not (currently) possible. Try to search before you ask, there is a recent thread on the topic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is technically possible to achieve the effect by overloading, but that limits the ability to use default arguments.
    Code:
    void fun(int, int, int, int = 40)
    {
         // whatever
    }
    
    void fun(int a, int b)
    {
        fun(a, 20, b);
    }
    
    void fun(int a = 10)
    {
        fun(a, 20, 30, 40);
    }
    This isn't a particularly good idea; at the least it will make it VERY easy for a user of these functions to make an error, and get the arguments in a wrong order. So there would be a maintenance nightmare associated with the any function that calls fun().

    IMHO, any coder who uses this sort of technique in production code that someone else has to reuse should be summarily shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 06:27 PM
  3. Default Arguments Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 05:27 PM
  4. Default Arguments
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 08:40 AM
  5. pointers as default arguments
    By ajm in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 03:26 PM