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>
=={{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
</
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");</
=={{header|Common Lisp}}==
The [http://www.lispworks.com/documentation/HyperSpec/Body/03_dac.htm <
(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);
}
}</
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!");</
Or with an array directly:
<lang java>Object[] args = {"Rosetta", "Code", "Is", "Awseome!"};
printAll(args);</
=={{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!");</
You can use the <
<lang javascript>args = ["Rosetta", "Code", "Is", "Awseome!"]
printAll.apply(null, args)</
=={{header|Logo}}==
Line 259:
=={{header|Perl}}==
Functions in Perl 5 don't have argument lists. All arguments are stored in the array <
<lang perl>sub print_all {
foreach (@_) {
print "$_\n";
}
}</
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!");</
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);</
=={{header|PHP}}==
PHP 4 and above supports varargs. You can deal with the argument list using the <
<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!");</
You can use the <
<lang php>$args = array("Rosetta", "Code", "Is", "Awseome!");
call_user_func_array('printAll', $args);</
=={{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</
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!")</
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)</
===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'
>>> </
=={{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</
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!")</
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)</
=={{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))</
Note that if you define the function anonymously using <
<lang scheme>(define print-all
(lambda things
(for-each
(lambda (x) (display x) (newline))
things)))</
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!")</
The <
<lang scheme>(define args '("Rosetta" "Code" "Is" "Awseome!"))
(apply print-all args)</
=={{header|V}}==
|