Why should default parameters be added last in C++ functions?
It is a matter about call convention. Call Convention: When you call a function, the parameters are pushed in stack from right to left. e.g.
fun(int a, int b, int c);
the stack is like this: a b c so, if you set the default value from left to right like this:
fun(int a = 1, int b = 2, int c);
and call like this:
fun(4,5);
your call means set a = 4, b = 5, and c = no value; // which is wrong!
if you declare the function like this:
fun(int a, int b = 2, int c = 3);
and call like this:
fun(4, 5);
your call means set a = 4, b = 5, and c = default value(3); // which is right!
In conclusion, you should put the default value from right to left.
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?
Jing Zeng is correct. I would like to add my remarks here. When a function is called, the arguments are pushed onto the stack from right to left. For example, let's say you have this arbitrary function.
int add(int a, int b) {
int c;
c = a + b;
return c;
}
Here is the stack frame for the function:
------
b
------
a
------
ret
------
c
------
This diagram above is the stack frame for this function! As you can see, first b is pushed onto the stack, then a is pushed onto the stack. After that the function return address is pushed onto the stack. The function return address holds the location in main() from where the function was originally called, and after the function is done executing, the execution of the program goes to that function's return address. Then any local variables such as c are pushed onto the stack.
Now the key thing is that arguments are pushed onto the stack from right to left. Basically any default parameters that are supplied are literal values, which are stored in the code section of an executable. When the program execution encounters a default parameter without a corresponding argument, it pushes that literal value onto the top of the stack. Then it looks at a and pushes the argument's value onto the top of the stack. The stack pointer always points to the top of the stack, your most recently pushed variable. So any literal values you pushed onto the stack as default parameters are "behind" the stack pointer.
It was probably more efficient for the compiler to quickly first push arbitrary default literal values onto the stack first, since they're not stored in a memory location, and build up the stack quickly. Think about what would have been if the variables would have been pushed onto the stack first, and then the literals. Accessing a memory location for the CPU takes up a relatively long time compared to pulling a literal value out of a circuit or CPU register. Since it takes more time to push variables onto the stack vs. literals, the literals would have to wait, then the return address would have to wait, and the local variables would have to wait as well. It's probably not a big concern in efficiency, but that's just my theory for why default arguments are always in the rightmost positions of a function header in C++. It means that the compiler was designed as such.
Another thing that the standards committee had to consider was how default parameters interacts with other features, like overloaded functions, template resolution, and name lookup. These features interact in very complex and hard to describe ways already. Making default parameters be able to appear anywhere would only increase the complexity.
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.
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.