Talk:Variadic function
From Wikipedia, the free encyclopedia
[edit] Optargs and kwargs aren't varargs
Removed from the article for discussion:
- Optional arguments with default values
- Besides the concept of a completely arbitrary number of arguments as discussed above, the declaration of default values for some of the specified arguments is another concept allowing to call a function with variable number of arguments. Several typed languages implement this, and can in some cases associate the arguments to the corresponding variables in view of their type, even if they are given at call-time in an order differing from the declaration. In languages which are not (strongly) typed (such as PHP), such optional arguments with default values cannot precede mandatory arguments without default value. (An exception are some system functions of PHP, like the implode function, that can associate arguments given in any order to the right variables, in view of their type.)
Optional arguments (optargs) do not make a function variadic, even though it may look that way if you examine only the calling code. When a function with optargs is executed, each of the args is bound to some value -- be it a supplied value from the function call, a default value from the function definition, or a null value in the absence of a default value.
The same is true for keyword arguments (kwargs), as in Python or Common Lisp -- a keyword argument gets populated with a value from the call, or it doesn't and defaults to a null value, but it's still bound in the environment under which the function-body is evaluated.
What's going on in variadic functions (varargs, rest args) is that passed-in arguments are not each used to supply a value for a variable. The handling of the list of passed-in arguments is under the control of the function code itself. Instead of binding a bunch of variables, the arguments are passed in as a list. (Or, in some languages, a list and a length.) This is why C bozos can walk off the end of a varargs list and end up smashing the stack. --FOo 23:17, 26 May 2005 (UTC)