Jump to content

Talk:Variadic function

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Optargs and kwargs aren't varargs

[edit]

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)[reply]

In the case of Python, isn't this a case of a distinction without a difference in that a function ends up with a variable number of arguments that it can access. Putting aside the default arguments, Python does have variadic functions. --Paddy (talk) 12:48, 23 June 2008 (UTC)[reply]

Variadic functions in C and C++

[edit]

The code example in this section is not commented at all, nor is there a caption explaining what its purpose is. This should be corrected. --Walkeraj 22:06, 3 October 2007 (UTC)[reply]

...and a C example would also be nice, especially because there exists the variadic macro article. --Abdull 09:17, 4 December 2007 (UTC)[reply]

I was hoping to find an example here of how to create a variadic function wrapping another such function.

Label *createLabel(Object *owner, const char * format, ...) {
  char labelDescription[200];
  snprintf(labelDescription, 200, format, /* What goes here?? */);
  return new Label(owner, labelDescription);
}

99.67.239.69 (talk) 01:09, 3 May 2011 (UTC)[reply]

Use vsnprintf(): https://en.cppreference.com/w/c/io/vfprintf --2003:E3:B725:FC00:71C9:1666:9CE0:20D9 (talk) 12:41, 11 May 2022 (UTC)[reply]

manual tag

[edit]

I've added the {{manual}} tag to the section on specific implementations. A brief, prose overview with perhaps one or two examples might be useful, but what we have is just a sprawling reference manual on how to use variadic parameter passing in multiple languages, and that's not what a Wikipedia article is for. -Miskaton (talk) 12:54, 11 April 2008 (UTC)[reply]

I agree. Considering that no-one has objected in the past 3 years, I'm going to remove all the notes about specific implementations -- each language which support variadic functions should have their own section about it, instead of having a large collection of semi-useful often poorly written sections here. 80.162.60.16 (talk) 13:08, 23 July 2011 (UTC)[reply]
I would prefer you remove them and instead reference the newly added Rosetta Code task from the external links section. --Paddy (talk) 06:31, 24 July 2011 (UTC)[reply]

Vararg function implementations can be divided into 3 classes

[edit]

What is the use of such a classification?
The list on its own is of very little use and should be deleted. --Paddy (talk) 05:35, 30 July 2010 (UTC)[reply]

Usefulness of vadiadic functions?

[edit]

Is there any advantage to using a vadiadic function over say, a list? Perhaps it's more useful in a language like C where you can't construct lists easily? What are the advantages/disadvantages? Language bloat? —Preceding unsigned comment added by 99.224.181.247 (talk) 00:16, 12 May 2011 (UTC)[reply]