Why should default parameters be added last in C++ functions?

11,857

Solution 1

To simplify the language definition and keep code readable.

void foo(int x = 2, int y);

To call that and take advantage of the default value, you'd need syntax like this:

foo(, 3);

Which was probably felt to be too weird. Another alternative is specifying names in the argument list:

foo(y : 3);

A new symbol would have to be used because this already means something:

foo(y = 3); // assign 3 to y and then pass y to foo.

The naming approach was considered and rejected by the ISO committee because they were uncomfortable with introducing a new significance to parameter names outside of the function definition.

If you're interested in more C++ design rationales, read The Design and Evolution of C++ by Stroustrup.

Solution 2

If you define the following function:

void foo( int a, int b = 0, int c );

How would you call the function and supply a value for a and c, but leave b as the default?

foo( 10, ??, 5 );

Unlike some other languages (eg, Python), function arguments in C/C++ can not be qualified by name, like the following:

foo( a = 10, c = 5 );

If that were possible, then the default arguments could be anywhere in the list.

Solution 3

Imagine you had a function with this prototype:

void testFunction(bool a = false, bool b = true, bool c);

Now suppose I called the function like this:

testFunction(true, false);

How is the compiler supposed to figure out which parameters I meant to supply values for?

Solution 4

As most of the answers point out, having default parameters potentially anywhere in the parameter list increases the complexity and ambiguity of function calls (for both the compiler and probably more importantly for the users of the function).

One nice thing about C++ is that there's often a way to do what you want (even if it's not always a good idea). If you want to have default arguments for various parameter positions, you can almost certainly do this by writing overloads that simply turn around and call the fully-parameterized function inline:

 int foo( int x, int y);
 int foo( int y) {
     return foo( 0, y);
 }

And there you have the equivalent of:

 int foo( int x = 0, int y);

Solution 5

Its because it uses the relative position of arguments to find to which parameters they correspond.

It could have used the types to identify that an optional parameter was not given. But implicit conversion could interfere with it. Another problem would be programming errors that could be interpreted as optional arguments drop out instead of missing argument error.

In order to allow any argument to become optional, there should be a way to identify the arguments to make sure there is no programming error or to remove ambiguities. This is possible in some languages, but not in C++.

Share:
11,857

Related videos on Youtube

yesraaj
Author by

yesraaj

Learning c++,Check out my blog

Updated on April 19, 2022

Comments

  • yesraaj
    yesraaj about 2 years

    Why should default parameters be added last in C++ functions?

    • Steve Jessop
      Steve Jessop almost 15 years
      It's not useless. Understanding language design (and in particular the design of the language(s) you use) should IMO be encouraged. To do this, among other things you need to find out why your ideas for "obvious, simple" improvements are impractical, if they are impractical. And why the designers of that language missed an opportunity, if they are not impractical. I'm pretty sure language design is related to programming.
  • juanchopanza
    juanchopanza over 8 years
    It would figure it out because there would be rules for that. For example, the arguments would bind to the second and third parameters.
  • phuclv
    phuclv over 5 years
    VB allows default parameters in the middle and you can just call foo(10, , 5) or foo 10,, 5. But it's ugly and less readable