Default argument in the middle of parameter list?
That code would work if in the very first declaration of the function, the last parameter has default value, something like this:
//declaration
void error(char const *msg, bool showKind, bool exit = false);
And then in the same scope you can provide default values for other arguments (from right side), in the later declaration, as:
void error(char const *msg, bool showKind = true, bool exit); //okay
//void error(char const *msg = 0 , bool showKind, bool exit); // error
which can called as:
error("some error messsage");
error("some error messsage", false);
error("some error messsage", false, true);
Online Demo : http://ideone.com/aFpUn
Note if you provide default value for the first parameter (from left), without providing default value for the second, it wouldn't compile (as expected) : http://ideone.com/5hj46
§8.3.6/4 says,
For non-template functions, default
arguments can be added in later
declarations of a function in the same
scope.
Example from the Standard itself:
void f(int, int);
void f(int, int = 7);
The second declaration adds default value!
Also see §8.3.6/6.
Default value parameter in Groovy as middle argument
In groovy you can assign default values to method's arguments, making those arguments optional.
Usually the optional arguments are the trailing ones, and that makes sence from the POV of calling such methods.
You could also declare default values for the middle arguments, like you did. In this case you should be aware of which arguments will get the defaults and which will not.
Consider the extension of your example:
def testMethod(arg1, arg2 = "arg2", arg3) {
println arg1
println arg2
println arg3
}
testMethod 1, 3
println '-----------'
def testMethod1(arg1, arg2 = "arg2", arg3 = 'arg3') {
println arg1
println arg2
println arg3
}
testMethod1 1,2
It prints:
1
arg2
3
-----------
1
2
arg3
So, when calling both methods with 2 arguments, the testMethod
replaces the 2nd arg with default, whereas the testMethod1
defaults the 3rd arg.
Why should default parameters be added last in C++ functions?
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.
Lisp - optional argument in the middle of parameter list
&optional
can only be at the end of the positional arguments (you can have &rest
or &body
after it). Even if you could put it earlier, if there's also &rest
or &body
, how would it know whether you provided the optional argument? E.g. if the lambda list were
(&optional arg1 arg2 &rest rest-args)
and the call were
(func-name 1 2 3 4 5)
it could either be arg1 = 1, arg2 = 2, rest-args = (3 4 5)
or arg1 = NIL, arg2 = 1, rest-args = (2 3 4 5)
.
You'll need to define the macro to take a single &rest
argument. Then you can check whether the first argument is a keyword or not, update the argument list to add the default, and then parse it with destructuring-bind
.
(defmacro some-macro (&rest all-args)
(unless (keywordp (first all-args))
(push nil all-args)) ;; flag defaults to NIL
(destructuring-bind (flag generic-name (&rest args) &body body) all-args
...))
Change only a specific Default Parameter on a function
When you pass a value for a particular parameter that has a default argument, you have to pass values for all the default parameters before it. Otherwise, the value you have passed will be taken as the value for the first default parameter.
So you have to do this:
newAddress = QInputDialog::getText(
0,
"Enter an Address to Validate",
"Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)",
QLineEdit::Normal,
QString(),
&ok);
You can leave out passing values for the parameters after bool *
parameter.
The C++ standard states in [dcl.fct.default]/1
Default arguments will be used in calls where trailing arguments are missing.
Lifetime of default function arguments in python
As the argument is an attribute of the function object, it normally has the same lifetime as the function. Usually, functions exist from the moment their module is loaded, until the interpreter exits.
However, Python functions are first-class objects and you can delete all references (dynamically) to the function early. The garbage collector may then reap the function and subsequently the default argument:
>>> def foo(bar, spam=[]):
... spam.append(bar)
... print(spam)
...
>>> foo
<function foo at 0x1088b9d70>
>>> foo('Monty')
['Monty']
>>> foo('Python')
['Monty', 'Python']
>>> foo.func_defaults
(['Monty', 'Python'],)
>>> del foo
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
Note that you can directly reach the func_defaults
attribute (__defaults__
in python 3), which is writable, so you can clear the default by reassigning to that attribute.
Swift: function with default parameter before non-default parameter
The current compiler does allow default parameters in the middle of a parameter list.
You can call the function like this if you want to use the default value for the first
parameter:
f(1)
If you want to supply a new value for the first
parameter, use its external name:
f(first: 3, 1)
The documentation explains that parameters with a default value are automatically given an external name:
Swift provides an automatic external name for any defaulted parameter you define, if you do not provide an external name yourself. The automatic external name is the same as the local name, as if you had written a hash symbol before the local name in your code.
Related Topics
How to Specify Setprecision Rounding
How to Choose Between Map and Unordered_Map
C++ Singleton VS. Global Static Object
How to Use Formatmessage() Properly in C++
How to Make Visual Studio Use the Native Amd64 Toolchain
What's the Difference Between Long Long and Long
How Portable Is End Iterator Decrement
Why Does Calling Method Through Null Pointer "Work" in C++
How Does Virtual Method Invocation Work in C++
Gcc Linker Can't Find Standard Library
How Boost::Function and Boost::Bind Work
How to Get Total CPU Usage in Linux Using C++
How to Determine the Size of an Object in C++