Variadic function: Difference between revisions

Content added Content deleted
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 13: Line 13:
*   [[Call a function]]
*   [[Call a function]]
<br><br>
<br><br>

=={{header|ACL2}}==
<lang Lisp>(defun print-all-fn (xs)
(if (endp xs)
nil
(prog2$ (cw "~x0~%" (first xs))
(print-all-fn (rest xs)))))

(defmacro print-all (&rest args)
`(print-all-fn (quote ,args)))</lang>

=={{header|ActionScript}}==
<lang actionscript>public function printArgs(... args):void
{
for (var i:int = 0; i < args.length; i++)
trace(args[i]);
}</lang>


=={{header|Ada}}==
=={{header|Ada}}==
Line 51: Line 68:
Output:<pre>Mary had a little lamb.
Output:<pre>Mary had a little lamb.
Rosetta Code is cooool!</pre>
Rosetta Code is cooool!</pre>

=={{header|ACL2}}==
<lang Lisp>(defun print-all-fn (xs)
(if (endp xs)
nil
(prog2$ (cw "~x0~%" (first xs))
(print-all-fn (rest xs)))))

(defmacro print-all (&rest args)
`(print-all-fn (quote ,args)))</lang>

=={{header|ActionScript}}==
<lang actionscript>public function printArgs(... args):void
{
for (var i:int = 0; i < args.length; i++)
trace(args[i]);
}</lang>


=={{header|Aime}}==
=={{header|Aime}}==
Line 159: Line 159:


For another example see [[Average/Simple_moving_average#ALGOL_68|Average/Simple moving average]]. This example is closer to the keyword arguments found in python.
For another example see [[Average/Simple_moving_average#ALGOL_68|Average/Simple moving average]]. This example is closer to the keyword arguments found in python.



=={{header|AppleScript}}==
=={{header|AppleScript}}==
Line 501: Line 500:
two
two
three
three
…À�”ÀƒÄ��¶À÷ØА¡<"A</pre>
…À�”ÀƒÄ��¶À÷ØÃ���¡<"A</pre>
{{works with|Beta BASIC|3.0}}
{{works with|Beta BASIC|3.0}}
{{works with|SAM BASIC}}
{{works with|SAM BASIC}}
Line 599: Line 598:


The actual implementation of <tt>va_list</tt> is implementation-dependent. If you are developing on a specific platform, you may use platform-specific knowledge to create a <tt>va_list</tt> by hand in a non-portable way. For example, on many platforms, a <tt>va_list</tt> is simply a pointer to a buffer where the arguments are arranged contiguously in memory.
The actual implementation of <tt>va_list</tt> is implementation-dependent. If you are developing on a specific platform, you may use platform-specific knowledge to create a <tt>va_list</tt> by hand in a non-portable way. For example, on many platforms, a <tt>va_list</tt> is simply a pointer to a buffer where the arguments are arranged contiguously in memory.

=={{header|C sharp|C#}}==

<lang csharp>using System;

class Program {
static void Main(string[] args) {
PrintAll("test", "rosetta code", 123, 5.6);
}

static void PrintAll(params object[] varargs) {
foreach (var i in varargs) {
Console.WriteLine(i);
}
}
}</lang>

Output:

<pre>test
rosetta code
123
5.6</pre>


=={{header|C++}}==
=={{header|C++}}==
Line 631: Line 653:
}</lang>
}</lang>
As the example shows, variadic templates allow any type to be passed.
As the example shows, variadic templates allow any type to be passed.

=={{header|C sharp|C#}}==

<lang csharp>using System;

class Program {
static void Main(string[] args) {
PrintAll("test", "rosetta code", 123, 5.6);
}

static void PrintAll(params object[] varargs) {
foreach (var i in varargs) {
Console.WriteLine(i);
}
}
}</lang>

Output:

<pre>test
rosetta code
123
5.6</pre>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 880: Line 879:


See for more info: http://dlang.org/function.html
See for more info: http://dlang.org/function.html

=={{header|Déjà Vu}}==

Variadic functions in the Déjà Vu standard library generally end with <code>(</code>, <code>[</code> or <code>{</code>. For this purpose, <code>)</code>, <code>]</code> and <code>}</code> are autonyms (that is, they have a global bindings to themselves, so that <code>)</code> is the same as <code>:)</code>).

<lang dejavu>show-all(:
while /= ) dup:
!.
drop

show-all( :foo "Hello" 42 [ true ] )</lang>
{{out}}
<pre>:foo
"Hello"
42
[ true ]</pre>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
Line 913: Line 896:
123
123
5.6</pre>
5.6</pre>

=={{header|Déjà Vu}}==

Variadic functions in the Déjà Vu standard library generally end with <code>(</code>, <code>[</code> or <code>{</code>. For this purpose, <code>)</code>, <code>]</code> and <code>}</code> are autonyms (that is, they have a global bindings to themselves, so that <code>)</code> is the same as <code>:)</code>).

<lang dejavu>show-all(:
while /= ) dup:
!.
drop

show-all( :foo "Hello" 42 [ true ] )</lang>
{{out}}
<pre>:foo
"Hello"
42
[ true ]</pre>


=={{header|E}}==
=={{header|E}}==
Line 1,029: Line 1,028:
print_each( Arguments ) -> [io:fwrite( "~p~n", [X]) || X <- Arguments].
print_each( Arguments ) -> [io:fwrite( "~p~n", [X]) || X <- Arguments].
</lang>
</lang>

=={{header|Euphoria}}==
<lang euphoria>procedure print_args(sequence args)
for i = 1 to length(args) do
puts(1,args[i])
puts(1,' ')
end for
end procedure

print_args({"Mary", "had", "a", "little", "lamb"})</lang>


=={{header|Euler Math Toolbox}}==
=={{header|Euler Math Toolbox}}==
Line 1,059: Line 1,048:
64
64
</lang>
</lang>

=={{header|Euphoria}}==
<lang euphoria>procedure print_args(sequence args)
for i = 1 to length(args) do
puts(1,args[i])
puts(1,' ')
end for
end procedure

print_args({"Mary", "had", "a", "little", "lamb"})</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 1,077: Line 1,076:
--- Data stack:
--- Data stack:
"apple"</lang>
"apple"</lang>

=={{header|Fōrmulæ}}==

In [https://wiki.formulae.org/Variadic_function this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Forth}}==
=={{header|Forth}}==
Line 1,182: Line 1,173:
TRUE
TRUE
!</lang>
!</lang>

=={{header|Fōrmulæ}}==

In [https://wiki.formulae.org/Variadic_function this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Go}}==
=={{header|Go}}==
Line 1,456: Line 1,455:
5
5
6</lang>
6</lang>




=={{header|Julia}}==
=={{header|Julia}}==
Line 1,924: Line 1,921:
}</lang>
}</lang>
It is valid for the @rest array to be empty, so this is also an optional parameter (see [[Optional parameters]]).
It is valid for the @rest array to be empty, so this is also an optional parameter (see [[Optional parameters]]).

=={{header|Perl 6}}==
{{works with|Rakudo|#25 "Minneapolis"}}

If a subroutine has no formal parameters but mentions the variables <code>@_</code> or <code>%_</code> in its body, it will accept arbitrary positional or keyword arguments, respectively. You can even use both in the same function.

<lang perl6>sub foo {
.say for @_;
say .key, ': ', .value for %_;
}

foo 1, 2, command => 'buckle my shoe',
3, 4, order => 'knock at the door';</lang>

This prints:

<pre>1
2
3
4
command: buckle my shoe
order: knock at the door</pre>

Perl 6 also supports slurpy arrays and hashes, which are formal parameters that consume extra positional and keyword arguments like <code>@_</code> and <code>%_</code>. You can make a parameter slurpy with the <code>*</code> twigil. This implementation of <code>&foo</code> works just like the last:

<lang perl6>sub foo (*@positional, *%named) {
.say for @positional;
say .key, ': ', .value for %named;
}</lang>

Unlike in Perl 5, arrays and hashes aren't flattened automatically. Use the <code>|</code> operator to flatten:

<lang perl6>foo |@ary, |%hsh;</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,017: Line 1,981:
printAll(...$args);
printAll(...$args);
?></lang>
?></lang>

=={{header|PL/I}}==
<lang pli>/* PL/I permits optional arguments, but not an infinitely varying */
/* argument list: */
s: procedure (a, b, c, d);
declare (a, b, c, d) float optional;
if ^omitted(a) then put skip list (a);
if ^omitted(b) then put skip list (b);
if ^omitted(c) then put skip list (c);
if ^omitted(d) then put skip list (d);
end s;</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 2,054: Line 2,007:
(d e f)
(d e f)
"hello"</pre>
"hello"</pre>

=={{header|PL/I}}==
<lang pli>/* PL/I permits optional arguments, but not an infinitely varying */
/* argument list: */
s: procedure (a, b, c, d);
declare (a, b, c, d) float optional;
if ^omitted(a) then put skip list (a);
if ^omitted(b) then put skip list (b);
if ^omitted(c) then put skip list (c);
if ^omitted(d) then put skip list (d);
end s;</lang>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 2,106: Line 2,070:
3
3
</pre>
</pre>

=={{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.
Line 2,218: Line 2,183:
14
14
</lang>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#25 "Minneapolis"}}

If a subroutine has no formal parameters but mentions the variables <code>@_</code> or <code>%_</code> in its body, it will accept arbitrary positional or keyword arguments, respectively. You can even use both in the same function.

<lang perl6>sub foo {
.say for @_;
say .key, ': ', .value for %_;
}

foo 1, 2, command => 'buckle my shoe',
3, 4, order => 'knock at the door';</lang>

This prints:

<pre>1
2
3
4
command: buckle my shoe
order: knock at the door</pre>

Perl 6 also supports slurpy arrays and hashes, which are formal parameters that consume extra positional and keyword arguments like <code>@_</code> and <code>%_</code>. You can make a parameter slurpy with the <code>*</code> twigil. This implementation of <code>&foo</code> works just like the last:

<lang perl6>sub foo (*@positional, *%named) {
.say for @positional;
say .key, ': ', .value for %named;
}</lang>

Unlike in Perl 5, arrays and hashes aren't flattened automatically. Use the <code>|</code> operator to flatten:

<lang perl6>foo |@ary, |%hsh;</lang>

=={{header|RapidQ}}==
RapidQ uses special keywords SUBI and FUNCTIONI for procedures and functions with variable number of parameters.
Numeric parameters are accessed from array ParamVal and string parameters from array ParamStr$.
<lang rapidq>SUBI printAll (...)
FOR i = 1 TO ParamValCount
PRINT ParamVal(i)
NEXT i
FOR i = 1 TO ParamStrCount
PRINT ParamStr$(i)
NEXT i
END SUBI
printAll 4, 3, 5, 6, 4, 3
printAll 4, 3, 5
printAll "Rosetta", "Code", "Is", "Awesome!"</lang>


=={{header|REALbasic}}==
=={{header|REALbasic}}==
Line 2,250: Line 2,265:


print-all [rebol works this way]</lang>
print-all [rebol works this way]</lang>

=={{header|RapidQ}}==
RapidQ uses special keywords SUBI and FUNCTIONI for procedures and functions with variable number of parameters.
Numeric parameters are accessed from array ParamVal and string parameters from array ParamStr$.
<lang rapidq>SUBI printAll (...)
FOR i = 1 TO ParamValCount
PRINT ParamVal(i)
NEXT i
FOR i = 1 TO ParamStrCount
PRINT ParamStr$(i)
NEXT i
END SUBI
printAll 4, 3, 5, 6, 4, 3
printAll 4, 3, 5
printAll "Rosetta", "Code", "Is", "Awesome!"</lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,515: Line 2,514:


0 OK, 0:236</pre>
0 OK, 0:236</pre>

=={{header|Unicon}}==
See [[#Icon|Icon]].


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 2,529: Line 2,531:
('x','y')
('x','y')
100</pre>
100</pre>

=={{header|Unicon}}==
See [[#Icon|Icon]].


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