Thread: default arguments

  1. #1
    Registered User
    Join Date
    Mar 2011

    default arguments

    The following text from a book:
    Default arguments are useful if you donít want to go to the trouble of writing arguments that, for example, almost always have the same value. They are also useful in cases where, after a program is written, the programmer decides to increase the capability of a function by adding another argument. Using default arguments means that the existing function calls can continue to use the old number of arguments, while new function calls can use more.
    Could you please explain it to me? I couldn't understand it. It would be really kind of you. Perhaps a simple example would make it clear.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    A simple example follows.

    getline - C++ Reference

    While this reference denotes two function signatures,

     istream &getline(istream &in, string &s, char delim = '\n');
    this does everything the reference says it does, but it uses default arguments.

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Default arguments are kind of like a shorter, faster way of overloading. The getline() example might look like this without default arguments:
    istream& getline(istream& in, string& s, char delim);
    istream& getline(istream& in, string& s)
        return getline(in, s, '\n');

  4. #4
    Registered User
    Join Date
    Feb 2011
    The purpose of default arguments is to avoid some tedious coding.
    Let's say for example that you are writing the function:
    double logarithm(double x, double base)
    Let's assume in your code, you call logorithm() many times, but usually you want to call it with base 10.
    double value1 = logarithm(3, 10);
    double value2 = logarithm(4, 10);
    double value3 = logarithm(123, 10);
    You might see it is rather redundant to keep writing the '10' as an additional parameter.
    Therefore, you might find it in your best interest to write the 'double base' parameter as
    a default parameter.
    double logarithm(double x, double base = 10)
    What this code says is: "This function is called logarithm which takes 2 parameters: a double
    named x, and another double named base. If a call to logarithm omits the second parameter,
    then treat it as if he/she passed a 10."
    This way, calls to logarithm can be:
    double value1 = logarithm(3);     // assumed base 10.
    double value2 = logarithm(4);     // assumed base 10.
    double value3 = logarithm(123);   // assumed base 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 06:27 PM
  2. default Arguments
    By kishore in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2007, 02:17 AM
  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