Variadic function
Create a function which takes in a variable number of arguments and prints each one on its own line.
You are encouraged to solve this task according to the task description, using any language you may know.
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.
#include <stdio.h> #include <stdarg.h> void varstrings(int count, ...) /* the ellipsis indicates variable arguments */ { va_list args; va_start(args, count); while (count--) puts(va_arg(args, char *)); va_end(args); }
varstrings(5, "Mary", "had", "a", "little", "lamb");
ALGOL 68
Variable arguments of arbitrarily typed values are not permitted in ALGOL 68. However a flexible array of tagged types (union) is permitted. This effectively allows the passing of strongly typed variable arguments to procedures.
main:( MODE STRINT = UNION(STRING, INT, PROC(REF FILE)VOID); PROC print strint = (FLEX[]STRINT argv)VOID: ( FOR i TO UPB argv DO CASE argv[i] IN (INT i):printf(($x1d$,i)), (STRING s):printf(($xg$,s)), (PROC(REF FILE)VOID f):f(stand out) ESAC OD ); print strint((new page,"Mary","had",1,"little","lamb",new line)) )
Output:
<newpage> Mary had 1 little lamb
Common Lisp
The &rest
lambda list keyword causes all remaining arguments to be bound to the following variable.
(defun example (&rest args) (dolist (arg args) (print arg)))
(example "Mary" "had" "a" "little" "lamb") (let ((args '("Mary" "had" "a" "little" "lamb"))) (apply #'example args))
E
Varargs is mildly unidiomatic in E, as the argument count is dispatched on, and often considered part of the method name.
However, accepting any number of arguments can easily be done, as it is just a particular case of the basic mechanism for dynamic message handling:
def example { match [`run`, args] { for x in args { println(x) } } }
example("Mary", "had", "a", "little", "lamb") E.call(example, "run", ["Mary", "had", "a", "little", "lamb"])
For comparison, a plain method doing the same thing for exactly two arguments would be like this:
def non_example { to run(x, y) { println(x) println(y) } }
or, written using the function syntax,
def non_example(x, y) { println(x) println(y) }
Forth
Words taking variable numbers of arguments may be written by specifying the number of parameters to operate upon as the top parameter. There are two standard words which operate this way: PICK and ROLL.
: sum ( x_1 ... x_n n -- sum ) 1 ?do + loop ; 4 3 2 1 4 sum . \ 10
Alternatively, you can operate upon the entire parameter stack for debugging by using the word DEPTH, which returns the number of items currently on the stack.
: .stack ( -- ) depth 0 ?do i pick . loop ;
Io
printAll := method(call message arguments foreach(println))
Java
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>
JavaScript
function varargs() { for (var i=0; i<arguments.length; i++) print(arguments[i]) }
Logo
UCB Logo allows four classes of arguments (in order):
- 0 or more required inputs (colon prefixed words)
- 0 or more optional inputs (two member lists: colon prefixed word with default value)
- an optional "rest" input (a list containing a colon prefixed word, set to the list of remaining arguments)
- ...with an optional default arity (a number)
to varargs [:args] foreach :args [print ?] end
(varargs "Mary "had "a "little "lamb) apply "varargs [Mary had a little lamb]
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>
Ruby
The * is sometimes referred to as the "splat" in Ruby. def print_all(*things)
things.each { |x| puts x }
end
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>
V
In V, all the arguments are passed in stack, and the stack is freely accessible so var args is the default to any level of functions
Using a count as the indication of number of arguments to extract,
[myfn [zero? not] [swap puts pred] while ].
100 200 300 400 500 3 myfn
results in 500 400 300