Many languages have the ability to pass any number of function arguments, but each has slightly different specifications.
In C language, printf
, which is used first in learning, is also composed of variable length arguments, but it is surprisingly difficult to create such a function.
Macros for variadic arguments are provided in a file called stdargs.h
.
va_start
… Initialize the argument pointerva_arg
… Get argumentsva_end
… Clean up the argument pointerThe characteristics of the implementation are as follows.
va_start
. printf
"" (If the pointer argument is followed, NULL
at the end` It is necessary to decide the value that indicates the end of the argument (such as inserting).JavaScript
In the case of JavaScript up to ES5, there is no special notation labeled as variable length argument, but it corresponds to the variable length argument itself because there is no problem even if the numbers do not match between the dummy argument and the actual argument. I can do it.
In a JavaScript function, the arguments are contained in an "array-like object" called ʻarguments, and you can get the number of arguments with ʻarguments.length
. In addition, when there is a formal argument, there was a behavior that the element of ʻarguments` at the same position as the formal argument is linked, but it is abolished in the strict mode of ES5.
You can just turn the subscript with for
, or if you want to convert it to a real array, you can do it with ʻArray.prototype.slice.call (arguments)`.
If you want to convert the contents of the array to an argument list, you can do it with function.apply (object to be this, array)
.
ES6
In ES6, it is possible to "receive a variable length argument as an array" and "expand an array etc. to the argument and pass it" with ...
(spread operator) similar to PHP. Note that in ES6's Arrow Function (function notation using =>
), ʻarguments` also ** inherits what is outside ** ([MDN](https://developer.mozilla. org / ja / docs / Web / JavaScript / Reference / arrow_functions)). Let's write it obediently with the spread operator.
Ruby
Ruby explicitly supports variadic arguments, and takes the exact number of arguments. When declaring a formal argument, if there is an argument prefixed with *
, it will receive the remaining arguments as an array (by nature, only one can be written in the argument list).
Number of arguments
def foo (arg1, *rest)
#Requires one or more arguments
end
def bar (*args)
#No problem
end
def baz (*args, last)
#Separately*The attached argument does not have to be the last
end
If you want to expand the array and use it as an argument, you can expand it by writing like method (* arr)
.
PHP
In the case of PHP, PHP 5.6 and later have the syntax ...
, and like *
in Ruby, it automatically packs variadic arguments into an array. You can also add type hints to this argument list.
Even in PHP 5.5 and earlier, the function can take more arguments than the formal arguments and can be processed by functions such as func_get_args
and func_num_args
.
If you want to expand an array as an argument, you can use ...
in PHP 5.6 and later, and even before that you can use call_user_func_array
.
Recommended Posts