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: Line 3:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<actionscript>
<lang actionscript>
public function printArgs(... args):void
public function printArgs(... args):void
{
{
Line 9: Line 9:
trace(args[i]);
trace(args[i]);
}
}
</lang>
</actionscript>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 53: 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.
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).
C calling convention has to be used (with keyword cdecl).
<freebasic>
<lang freebasic>
SUB printAll cdecl (count As Integer, ... )
SUB printAll cdecl (count As Integer, ... )
DIM arg AS Any Ptr
DIM arg AS Any Ptr
Line 66: Line 66:


printAll 3, 3.1415, 1.4142, 2.71828
printAll 3, 3.1415, 1.4142, 2.71828
</freebasic>
</lang>
For some reason, I was not able to get a Strings version of the above to work.
For some reason, I was not able to get a Strings version of the above to work.


Line 96: Line 96:
=={{header|C}}==
=={{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.
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.
<c>#include <stdio.h>
<lang c>#include <stdio.h>
#include <stdarg.h>
#include <stdarg.h>


Line 108: Line 108:
}
}


varstrings(5, "Mary", "had", "a", "little", "lamb");</c>
varstrings(5, "Mary", "had", "a", "little", "lamb");</lang>
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


The [http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm <code>&rest</code>] [http://www.lispworks.com/documentation/HyperSpec/Body/03_da.htm lambda list keyword] causes all remaining arguments to be bound to the following variable.
The [http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm <tt>&rest</tt>] [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)
(defun example (&rest args)
Line 217: Line 217:
{{works with|Java|1.5+}}
{{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.
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.
<java>public static void printAll(Object... things){
<lang java>public static void printAll(Object... things){
for(Object i:things){
for(Object i:things){
System.out.println(i);
System.out.println(i);
}
}
}</java>
}</lang>
This function can be called with any number of arguments:
This function can be called with any number of arguments:
<java>printAll(4, 3, 5, 6, 4, 3);
<lang java>printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</java>
printAll("Rosetta", "Code", "Is", "Awseome!");</lang>


Or with an array directly:
Or with an array directly:
<java>Object[] args = {"Rosetta", "Code", "Is", "Awseome!"};
<lang java>Object[] args = {"Rosetta", "Code", "Is", "Awseome!"};
printAll(args);</java>
printAll(args);</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<javascript>function printAll() {
<lang javascript>function printAll() {
for (var i=0; i<arguments.length; i++)
for (var i=0; i<arguments.length; i++)
print(arguments[i])
print(arguments[i])
Line 238: Line 238:
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</javascript>
printAll("Rosetta", "Code", "Is", "Awseome!");</lang>


You can use the <code>apply</code> method of a function to apply it to a list of arguments:
You can use the <tt>apply</tt> method of a function to apply it to a list of arguments:
<javascript>args = ["Rosetta", "Code", "Is", "Awseome!"]
<lang javascript>args = ["Rosetta", "Code", "Is", "Awseome!"]
printAll.apply(null, args)</javascript>
printAll.apply(null, args)</lang>


=={{header|Logo}}==
=={{header|Logo}}==
Line 259: Line 259:


=={{header|Perl}}==
=={{header|Perl}}==
Functions in Perl 5 don't have argument lists. All arguments are stored in the array <code>@_</code> anyway, so there is variable arguments by default.
Functions in Perl 5 don't have argument lists. All arguments are stored in the array <tt>@_</tt> anyway, so there is variable arguments by default.


<perl>sub print_all {
<lang perl>sub print_all {
foreach (@_) {
foreach (@_) {
print "$_\n";
print "$_\n";
}
}
}</perl>
}</lang>


This function can be called with any number of arguments:
This function can be called with any number of arguments:
<perl>print_all(4, 3, 5, 6, 4, 3);
<lang perl>print_all(4, 3, 5, 6, 4, 3);
print_all(4, 3, 5);
print_all(4, 3, 5);
print_all("Rosetta", "Code", "Is", "Awseome!");</perl>
print_all("Rosetta", "Code", "Is", "Awseome!");</lang>


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:
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:
<perl>@args = ("Rosetta", "Code", "Is", "Awseome!");
<lang perl>@args = ("Rosetta", "Code", "Is", "Awseome!");
print_all(@args);</perl>
print_all(@args);</lang>


=={{header|PHP}}==
=={{header|PHP}}==
PHP 4 and above supports varargs. You can deal with the argument list using the <code>func_num_args()</code>, <code>func_get_arg()</code>, and <code>func_get_args()</code> functions.
PHP 4 and above supports varargs. You can deal with the argument list using the <tt>func_num_args()</tt>, <tt>func_get_arg()</tt>, and <tt>func_get_args()</tt> functions.
<php>function printAll() {
<lang php>function printAll() {
foreach (func_get_args() as $x) // first way
foreach (func_get_args() as $x) // first way
echo "$x\n";
echo "$x\n";
Line 288: Line 288:
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5, 6, 4, 3);
printAll(4, 3, 5);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</php>
printAll("Rosetta", "Code", "Is", "Awseome!");</lang>


You can use the <code>call_user_func_array</code> function to apply it to a list of arguments:
You can use the <tt>call_user_func_array</tt> function to apply it to a list of arguments:
<php>$args = array("Rosetta", "Code", "Is", "Awseome!");
<lang php>$args = array("Rosetta", "Code", "Is", "Awseome!");
call_user_func_array('printAll', $args);</php>
call_user_func_array('printAll', $args);</lang>


=={{header|Python}}==
=={{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.
Putting <tt>*</tt> before an argument will take in any number of arguments and put them all in a tuple with the given name.


<python>def print_all(*things):
<lang python>def print_all(*things):
for x in things:
for x in things:
print x</python>
print x</lang>


This function can be called with any number of arguments:
This function can be called with any number of arguments:
<python>print_all(4, 3, 5, 6, 4, 3)
<lang python>print_all(4, 3, 5, 6, 4, 3)
print_all(4, 3, 5)
print_all(4, 3, 5)
print_all("Rosetta", "Code", "Is", "Awseome!")</python>
print_all("Rosetta", "Code", "Is", "Awseome!")</lang>


You can use the same "*" syntax to apply the function to an existing list of arguments:
You can use the same "*" syntax to apply the function to an existing list of arguments:
<python>args = ["Rosetta", "Code", "Is", "Awseome!"]
<lang python>args = ["Rosetta", "Code", "Is", "Awseome!"]
print_all(*args)</python>
print_all(*args)</lang>


===Keyword arguments ===
===Keyword arguments ===
Python also has keyword arguments were you can add arbitrary ''func('''''keyword1=value1, keyword2=value2 ...''''')'' keyword-value pairs when calling a function.
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.
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.
<python>>>> def printargs(*positionalargs, **keywordargs):
<lang python>>>> def printargs(*positionalargs, **keywordargs):
print "POSITIONAL ARGS:\n " + "\n ".join(repr(x) for x in positionalargs)
print "POSITIONAL ARGS:\n " + "\n ".join(repr(x) for x in positionalargs)
print "KEYWORD ARGS:\n " + '\n '.join(
print "KEYWORD ARGS:\n " + '\n '.join(
Line 337: Line 337:
'fee' = 'fi'
'fee' = 'fi'
'fo' = 'fum'
'fo' = 'fum'
>>> </python>
>>> </lang>


=={{header|RapidQ}}==
=={{header|RapidQ}}==
Line 359: Line 359:
=={{header|Ruby}}==
=={{header|Ruby}}==
The * is sometimes referred to as the "splat" in Ruby.
The * is sometimes referred to as the "splat" in Ruby.
<ruby>def print_all(*things)
<lang ruby>def print_all(*things)
things.each { |x| puts x }
things.each { |x| puts x }
end</ruby>
end</lang>


This function can be called with any number of arguments:
This function can be called with any number of arguments:
<ruby>print_all(4, 3, 5, 6, 4, 3)
<lang ruby>print_all(4, 3, 5, 6, 4, 3)
print_all(4, 3, 5)
print_all(4, 3, 5)
print_all("Rosetta", "Code", "Is", "Awseome!")</ruby>
print_all("Rosetta", "Code", "Is", "Awseome!")</lang>


You can use the same "*" syntax to apply the function to an existing list of arguments:
You can use the same "*" syntax to apply the function to an existing list of arguments:
<ruby>args = ["Rosetta", "Code", "Is", "Awseome!"]
<lang ruby>args = ["Rosetta", "Code", "Is", "Awseome!"]
print_all(*args)</ruby>
print_all(*args)</lang>


=={{header|Scheme}}==
=={{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.
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.


<scheme>(define (print-all . things)
<lang scheme>(define (print-all . things)
(for-each
(for-each
(lambda (x) (display x) (newline))
(lambda (x) (display x) (newline))
things))</scheme>
things))</lang>


Note that if you define the function anonymously using <code>lambda</code>, 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:
Note that if you define the function anonymously using <tt>lambda</tt>, 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:


<scheme>(define print-all
<lang scheme>(define print-all
(lambda things
(lambda things
(for-each
(for-each
(lambda (x) (display x) (newline))
(lambda (x) (display x) (newline))
things)))</scheme>
things)))</lang>


This function can be called with any number of arguments:
This function can be called with any number of arguments:
<scheme>(print-all 4 3 5 6 4 3)
<lang scheme>(print-all 4 3 5 6 4 3)
(print-all 4 3 5)
(print-all 4 3 5)
(print-all "Rosetta" "Code" "Is" "Awseome!")</scheme>
(print-all "Rosetta" "Code" "Is" "Awseome!")</lang>


The <code>apply</code> function will apply the function to a list of arguments:
The <tt>apply</tt> function will apply the function to a list of arguments:
<scheme>(define args '("Rosetta" "Code" "Is" "Awseome!"))
<lang scheme>(define args '("Rosetta" "Code" "Is" "Awseome!"))
(apply print-all args)</scheme>
(apply print-all args)</lang>


=={{header|V}}==
=={{header|V}}==