Variadic function: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 13:
*   [[Call a function]]
<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}}==
Line 51 ⟶ 68:
Output:<pre>Mary had a little lamb.
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}}==
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.
 
 
=={{header|AppleScript}}==
Line 501 ⟶ 500:
two
three
…À�”ÀƒÄ��¶À÷ØАÀ÷ØÃ���¡<"A</pre>
{{works with|Beta BASIC|3.0}}
{{works with|SAM BASIC}}
Line 599 ⟶ 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.
 
=={{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++}}==
Line 631 ⟶ 653:
}</lang>
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}}==
Line 880 ⟶ 879:
 
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}}==
Line 913 ⟶ 896:
123
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}}==
Line 1,029 ⟶ 1,028:
print_each( Arguments ) -> [io:fwrite( "~p~n", [X]) || X <- Arguments].
</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}}==
Line 1,059 ⟶ 1,048:
64
</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}}==
Line 1,077 ⟶ 1,076:
--- Data stack:
"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}}==
Line 1,182 ⟶ 1,173:
TRUE
!</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}}==
Line 1,456 ⟶ 1,455:
5
6</lang>
 
 
 
=={{header|Julia}}==
Line 1,924 ⟶ 1,921:
}</lang>
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}}==
Line 2,017 ⟶ 1,981:
printAll(...$args);
?></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}}==
Line 2,054 ⟶ 2,007:
(d e f)
"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}}==
Line 2,106 ⟶ 2,070:
3
</pre>
 
=={{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.
Line 2,218 ⟶ 2,183:
14
</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}}==
Line 2,250 ⟶ 2,265:
 
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}}==
Line 2,515 ⟶ 2,514:
 
0 OK, 0:236</pre>
 
=={{header|Unicon}}==
See [[#Icon|Icon]].
 
=={{header|Ursala}}==
Line 2,529 ⟶ 2,531:
('x','y')
100</pre>
 
=={{header|Unicon}}==
See [[#Icon|Icon]].
 
=={{header|V}}==
10,327

edits