Question About a Function Definition (Three Dots in Parameters..)

What does the three dots in the parameter list of a function mean?

These type of functions are called variadic functions (Wikipedia link). They use ellipses (i.e., three dots) to indicate that there is a variable number of arguments that the function can process. One place you've probably used such functions (perhaps without realising) is with the various printf functions, for example (from the ISO standard):

int printf(const char * restrict format, ...);

The ellipses allow you to create functions where the number of parameters are not known beforehand, and you can use stdargs.h functions (va_start, va_arg and va_end) to get the specific arguments.

You do have to know the types of the arguments you extract and have some way of deciding when you're done. The printf functions do this with the format string (for both types and count), while my example code below always assumes const char * as the type with a sentinel value NULL to decide completion.

This link here has a good treatise on the use of variable argument lists in printf.


As an example, the following program contains a function outStrings(), that allows you to print an arbitrary number of strings:

#include <stdio.h>
#include <stdarg.h>

void outStrings(const char *strFirst, ...) {
// First argument handled specially.

printf("%s", strFirst);
va_list pArg;
va_start(pArg, strFirst);

// Just get and process each string until NULL given.

const char *strNext = va_arg(pArg, const char *);
while (strNext != NULL) {
printf("%s", strNext);
strNext = va_arg(pArg, const char *);
}

// Finalise processing.

va_end(pArg);
}

int main(void) {
char *name = "paxdiablo";
outStrings("Hello, ", name, ", I hope you're feeling well today.\n", NULL);
}

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

It means that zero or more String objects (or a single array of them) may be passed as the argument(s) for that method.

See the "Arbitrary Number of Arguments" section here: http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html#varargs

In your example, you could call it as any of the following:

myMethod(); // Likely useless, but possible
myMethod("one", "two", "three");
myMethod("solo");
myMethod(new String[]{"a", "b", "c"});

Important Note: The argument(s) passed in this way is always an array - even if there's just one. Make sure you treat it that way in the method body.

Important Note 2: The argument that gets the ... must be the last in the method signature. So, myMethod(int i, String... strings) is okay, but myMethod(String... strings, int i) is not okay.

Thanks to Vash for the clarifications in his comment.

what is the meanining of 3 dots in function parameters?

This is not a Android feature. This is an Java feature (added in Java 5) that was included so you can have "custom" arguments.

This method:

protected Long doInBackground(URL... urls) {
for (URL url : urls) {
// Do something with url
}
}

and this one:

protected Long doInBackground(URL[] urls) {
for (URL url : urls) {
// Do something with url
}
}

for the internal method are the same.
The whole difference is on the way you invoke that.
For the first one (also known as varargs) you can simply do this:

doInBackground(url1,url2,url3,url4);

But for the second on you have to create a array, so if you try to do this in just one line it will be like:

doInBackground(new URL[] { url1, url2, url3 });

The good thing is that if you try to call the method that was written using varargs on that way it will work at the same way that if it wasn't (backward support).

What is the meaning of foo(...arg) (three dots in a function call)?

This has nothing to do with jQuery or Angular. It's a feature that was introduced in ES2015.

This particular use of ... doesn't actually have an official name. A name that is in line with other uses would be "spread argument" (a generic term would be "spread syntax"). It "explodes" (spreads) an iterable and passes each value as argument to the function. Your example is equivalent to:

this.heroes.push.apply(this.heroes, Array.from(heroes));

Besides being more concise, another advantage of ... here is that it can be more easily used with other concrete arguments:

func(first, second, ...theRest);

// as opposed to the following or something similar:
func.apply(null, [first, second].concat(Array.from(heroes)));

In a C function declaration, what does ... as the last parameter do?

it allows a variable number of arguments of unspecified type (like printf does).

you have to access them with va_start, va_arg and va_end

see http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html for more information

What is the meaning of three dots (...) in PHP?

This is literally called the ... operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:

This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:

function concatenate($transform, ...$strings) {
$string = '';
foreach($strings as $piece) {
$string .= $piece;
}
return($transform($string)); }

echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

(This would print I'D LIKE 6 APPLES)

The parameters list in the function declaration has the ... operator in it, and it basically means " ... and everything else should go into $strings". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array, ready to be used.

What is the meaning of ...args (three dots) in a function definition?

With respect to (...args) =>, ...args is a rest parameter. It always has to be the last entry in the parameter list and it will be assigned an array that contains all arguments that haven't been assigned to previous parameters.

It's basically the replacement for the arguments object. Instead of writing

function max() {
var values = Array.prototype.slice.call(arguments, 0);
// ...
}
max(1,2,3);

you can write

function max(...value) {
// ...
}
max(1,2,3);

Also, since arrow functions don't have an arguments object, this is the only way to create variadic (arrow) functions.


As controller.update(...args), see What is the meaning of "foo(...arg)" (three dots in a function call)? .

What do three dots (...) indicate when used as a part of parameters during method definition?

It's called varargs.

It means you can pass as many of that type as you want.

It actually translates it into method1(Animal[] a) and you reference them as a[1] like you would any other array.

If I have the following

Cat whiskers = new Cat();
Dog rufus = new Dog();
Dolphin flipper = new Dolphin();

method1(whiskers, rufus, flipper); // okay!
method1(rufus); // okay!
method1(); // okay!
method1(flipper,new Parakeet()); // okay!

Function with three dots argument

A function with three dots means, that you can pass a variable number of arguments. Since the called function doesn't really know how many arguments were passed, you usually need some way to tell this. So some extra parameter would be needed which you can use to determine the arguments.

A good example would be printf. You can pass any number of arguments, and the first argument is a string, which describes the extra parameters being passed in.

void func(int count, ...)
{
va_list args;
int i;
int sum = 0;

va_start(args, count);
for(i = 0; i < count; i++)
sum += va_arg(args, int);
va_end(ap);

printf("%d\n", sum);
}

update

To address your comment, you don't need the names of the arguments. That is the whole point of it, because you don't know at compile time which and how many arguments you will pass. That depends on the function of course. In my above example, I was assuming that only ints are passed though. As you know from printf, you pass any type, and you have to interpret them. that is the reason why you need a format specifier that tells the function what kind of parameter is passed. Or as shown in my example you can of course assume a specific type and use that.



Related Topics



Leave a reply



Submit