Variadic function: Difference between revisions

Content deleted Content added
m →‎[[Varargs#ALGOL 68]]: removed printf, space and new page procedure as these are not in ELLA and can be moved without detracting from the code sample.
No edit summary
Line 3:
 
=={{header|ActionScript}}==
<lang actionscript>
public function printArgs(... args):void
{
Line 9:
trace(args[i]);
}
</lang>
</actionscript>
 
=={{header|ALGOL 68}}==
Line 53:
The parameter list does not pass information about parameter type. If necessary, the type information has to be passed for example in the first parameter.
C calling convention has to be used (with keyword cdecl).
<lang freebasic>
SUB printAll cdecl (count As Integer, ... )
DIM arg AS Any Ptr
Line 66:
 
printAll 3, 3.1415, 1.4142, 2.71828
</freebasiclang>
For some reason, I was not able to get a Strings version of the above to work.
 
Line 96:
=={{header|C}}==
The ANSI C standard header stdarg.h defines macros for low-level access to the parameter stack. It does not know the number or types of these parameters; this is specified by the required initial parameter(s). For example, it could be a simple count or a more complicated parameter specification, like a printf() format string.
<lang c>#include <stdio.h>
#include <stdarg.h>
 
Line 108:
}
 
varstrings(5, "Mary", "had", "a", "little", "lamb");</clang>
=={{header|Common Lisp}}==
 
The [http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm <codett>&rest</codett>] [http://www.lispworks.com/documentation/HyperSpec/Body/03_da.htm lambda list keyword] causes all remaining arguments to be bound to the following variable.
 
(defun example (&rest args)
Line 217:
{{works with|Java|1.5+}}
Using <tt>...</tt> after the type of argument will take in any number of arguments and put them all in one array of the given type with the given name.
<lang java>public static void printAll(Object... things){
for(Object i:things){
System.out.println(i);
}
}</javalang>
This function can be called with any number of arguments:
<lang java>printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</javalang>
 
Or with an array directly:
<lang java>Object[] args = {"Rosetta", "Code", "Is", "Awseome!"};
printAll(args);</javalang>
 
=={{header|JavaScript}}==
<lang javascript>function printAll() {
for (var i=0; i<arguments.length; i++)
print(arguments[i])
Line 238:
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</javascriptlang>
 
You can use the <codett>apply</codett> method of a function to apply it to a list of arguments:
<lang javascript>args = ["Rosetta", "Code", "Is", "Awseome!"]
printAll.apply(null, args)</javascriptlang>
 
=={{header|Logo}}==
Line 259:
 
=={{header|Perl}}==
Functions in Perl 5 don't have argument lists. All arguments are stored in the array <codett>@_</codett> anyway, so there is variable arguments by default.
 
<lang perl>sub print_all {
foreach (@_) {
print "$_\n";
}
}</perllang>
 
This function can be called with any number of arguments:
<lang perl>print_all(4, 3, 5, 6, 4, 3);
print_all(4, 3, 5);
print_all("Rosetta", "Code", "Is", "Awseome!");</perllang>
 
Since lists are flattened when placed in a list context, you can just pass an array in as an argument and all its elements will become separate arguments:
<lang perl>@args = ("Rosetta", "Code", "Is", "Awseome!");
print_all(@args);</perllang>
 
=={{header|PHP}}==
PHP 4 and above supports varargs. You can deal with the argument list using the <codett>func_num_args()</codett>, <codett>func_get_arg()</codett>, and <codett>func_get_args()</codett> functions.
<lang php>function printAll() {
foreach (func_get_args() as $x) // first way
echo "$x\n";
Line 288:
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</phplang>
 
You can use the <codett>call_user_func_array</codett> function to apply it to a list of arguments:
<lang php>$args = array("Rosetta", "Code", "Is", "Awseome!");
call_user_func_array('printAll', $args);</phplang>
 
=={{header|Python}}==
Putting <tt>*</tt> before an argument will take in any number of arguments and put them all in a tuple with the given name.
 
<lang python>def print_all(*things):
for x in things:
print x</pythonlang>
 
This function can be called with any number of arguments:
<lang python>print_all(4, 3, 5, 6, 4, 3)
print_all(4, 3, 5)
print_all("Rosetta", "Code", "Is", "Awseome!")</pythonlang>
 
You can use the same "*" syntax to apply the function to an existing list of arguments:
<lang python>args = ["Rosetta", "Code", "Is", "Awseome!"]
print_all(*args)</pythonlang>
 
===Keyword arguments ===
Python also has keyword arguments were you can add arbitrary ''func('''''keyword1=value1, keyword2=value2 ...''''')'' keyword-value pairs when calling a function.
This example shows both keyword arguments and positional arguments. The two calls to the function are equivalent. '''*alist''' spreads the members of the list to create positional arguments, and '''**adict''' does similar for the keyword/value pairs from the dictionary.
<lang python>>>> def printargs(*positionalargs, **keywordargs):
print "POSITIONAL ARGS:\n " + "\n ".join(repr(x) for x in positionalargs)
print "KEYWORD ARGS:\n " + '\n '.join(
Line 337:
'fee' = 'fi'
'fo' = 'fum'
>>> </pythonlang>
 
=={{header|RapidQ}}==
Line 359:
=={{header|Ruby}}==
The * is sometimes referred to as the "splat" in Ruby.
<lang ruby>def print_all(*things)
things.each { |x| puts x }
end</rubylang>
 
This function can be called with any number of arguments:
<lang ruby>print_all(4, 3, 5, 6, 4, 3)
print_all(4, 3, 5)
print_all("Rosetta", "Code", "Is", "Awseome!")</rubylang>
 
You can use the same "*" syntax to apply the function to an existing list of arguments:
<lang ruby>args = ["Rosetta", "Code", "Is", "Awseome!"]
print_all(*args)</rubylang>
 
=={{header|Scheme}}==
Putting a dot before the last argument will take in any number of arguments and put them all in a list with the given name.
 
<lang scheme>(define (print-all . things)
(for-each
(lambda (x) (display x) (newline))
things))</schemelang>
 
Note that if you define the function anonymously using <codett>lambda</codett>, and you want all the args to be collected in one list (i.e. you have no parameters before the parameter that collects everything), then you can just replace the parentheses altogether with that parameter, as if to say, let this be the argument list:
 
<lang scheme>(define print-all
(lambda things
(for-each
(lambda (x) (display x) (newline))
things)))</schemelang>
 
This function can be called with any number of arguments:
<lang scheme>(print-all 4 3 5 6 4 3)
(print-all 4 3 5)
(print-all "Rosetta" "Code" "Is" "Awseome!")</schemelang>
 
The <codett>apply</codett> function will apply the function to a list of arguments:
<lang scheme>(define args '("Rosetta" "Code" "Is" "Awseome!"))
(apply print-all args)</schemelang>
 
=={{header|V}}==