Questions tagged [variadic-functions]

A function is variadic if it can accept a variable number of arguments; that is, its arity is not fixed. The abbreviation "varargs" is commonly used to refer to these types of functions.

Variadic functions are functions which accept a variable number of arguments—for example, a function that may be called with either 1 or 2 arguments.

Most high level languages have support for variadic functions within the language's syntax proper. Some of these languages do not require extra syntax (for example, JavaScript, Perl, and PHP). They typically access variable arguments through some sort of default variable (Perl uses _@).

Most compiled languages (for example, D, Java, C#, and VB.NET) and even some interpreted languages (for example, Python, CoffeeScript, Ruby, and Scheme) require explicit syntax in order to use variadic functions. They typically "roll" all excess arguments into a specially noted argument which can be accessed like an array (Python uses the *args syntax, where *args is a tuple object).

C and C++ variadic functions use the standard library (<stdarg.h>, C++ alternatively <cstdarg>).
They use the va_* functions in order to unpack and access variable numbers of arguments. See man stdarg or ANSI section 2.10 for more information on these functions.

2645 questions
3315
votes
27 answers

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

What do *args and **kwargs mean in these function definitions? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass See What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? for the complementary question…
Todd
  • 34,500
  • 4
  • 23
  • 13
1033
votes
9 answers

What do 3 dots next to a parameter type mean in Java?

What do the 3 dots following String in the following method mean? public void myMethod(String... strings) { // method body }
Vikram
  • 11,885
  • 5
  • 22
  • 16
803
votes
9 answers

Concatenate two slices in Go

I'm trying to combine the slice [1, 2] and the slice [3, 4]. How can I do this in Go? I tried: append([]int{1,2}, []int{3,4}) but got: cannot use []int literal (type []int) as type int in append However, the documentation seems to indicate this is…
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
538
votes
6 answers

Possible heap pollution via varargs parameter

I understand this occurs with Java 7 when using varargs with a generic type; But my question is.. What exactly does Eclipse mean when it says "its use could potentially pollute the heap?" And How does the new @SafeVarargs annotation prevent this?
hertzsprung
  • 9,445
  • 4
  • 42
  • 77
461
votes
21 answers

How can I convert the "arguments" object to an array in JavaScript?

The arguments object in JavaScript is an odd wart—it acts just like an array in most situations, but it's not actually an array object. Since it's really something else entirely, it doesn't have the useful functions from Array.prototype like…
Andrew Coleson
  • 10,100
  • 8
  • 32
  • 30
399
votes
17 answers

Variable number of arguments in C++?

How can I write a function that accepts a variable number of arguments? Is this possible, how?
nunos
  • 20,479
  • 50
  • 119
  • 154
361
votes
11 answers

Why use the params keyword?

I know this is a basic question, but I couldn't find an answer. Why use it? if you write a function or a method that's using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int…
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
360
votes
6 answers

Can I pass an array as arguments to a method with variable arguments in Java?

I'd like to be able to create a function like: class A { private String extraVar; public String myFormat(String format, Object ... args){ return String.format(format, extraVar, args); } } The problem here is that args is treated as…
user362382
  • 3,954
  • 2
  • 17
  • 11
359
votes
11 answers

Passing variable number of arguments around

Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function? Example: void…
Vicent Marti
  • 7,203
  • 6
  • 30
  • 34
344
votes
5 answers

How to pass an ArrayList to a varargs method parameter?

Basically I have an ArrayList of locations: ArrayList locations = new ArrayList(); below this I call the following method: .getMap(); the parameters in the getMap() method are: getMap(WorldLocation...…
MJ93
  • 4,966
  • 8
  • 32
  • 50
207
votes
8 answers

When do you use varargs in Java?

I'm afraid of varargs. I don't know what to use them for. Plus, it feels dangerous to let people pass as many arguments as they want. What's an example of a context that would be a good place to use them?
Harry Quince
  • 2,394
  • 3
  • 15
  • 11
203
votes
3 answers

Java SafeVarargs annotation, does a standard or best practice exist?

I've recently come across the java @SafeVarargs annotation. Googling for what makes a variadic function in Java unsafe left me rather confused (heap poisoning? erased types?), so I'd like to know a few things: What makes a variadic Java function…
Oren
  • 4,152
  • 3
  • 30
  • 37
195
votes
12 answers

Is it possible to send a variable number of arguments to a JavaScript function?

Is it possible to send a variable number of arguments to a JavaScript function, from an array? var arr = ['a','b','c'] var func = function() { // debug alert(arguments.length); // for(arg in arguments) …
Fire Crow
  • 7,499
  • 4
  • 36
  • 35
194
votes
12 answers

How to properly match varargs in Mockito

I've been trying to get to mock a method with vararg parameters using Mockito: interface A { B b(int x, int y, C... c); } A a = mock(A.class); B b = mock(B.class); when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b); assertEquals(b,…
qualidafial
  • 6,674
  • 3
  • 28
  • 38
177
votes
6 answers

Java variable number of arguments for a method

Is it possible to declare a method that will allow a variable number of parameters ? What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters? Answer: varargs
user69514
  • 26,935
  • 59
  • 154
  • 188
1
2 3
99 100