Variadic function: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created task...I think this is an OK name for it)
 
(added perl, python, scheme)
Line 13: Line 13:
printAll(4, 3, 5);
printAll(4, 3, 5);
printAll("Rosetta", "Code", "Is", "Awseome!");</java>
printAll("Rosetta", "Code", "Is", "Awseome!");</java>

=={{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.

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

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

=={{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.

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

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

=={{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.

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

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

Revision as of 04:33, 2 June 2008

Task
Variadic function
You are encouraged to solve this task according to the task description, using any language you may know.

Create a function which takes in a variable number of arguments and prints each one on its own line.

Java

Works with: Java version 1.5+

Using ... 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){

  for(Object i:things){
     System.out.println(i);
  }

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

Perl

Functions in Perl 5 don't have argument lists. All arguments are stored in the array @_ anyway, so there is variable arguments by default.

<perl>sub print_all {

 foreach (@_) {
   print "$_\n";
 }

}</perl>

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

Python

Putting * 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):

   for x in things:
       print x</python>

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

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.

<scheme>(define (print-all . things)

   (for-each
       (lambda (x) (display x) (newline))
       things))</scheme>

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