Named parameters: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Omit from rust)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 62:
Details: a Tyrannosaurus Dinosaur named Rex owned by George W. Bush
</pre>
 
=={{header|AppleScript}}==
AppleScript does not implement default or optional parameters, but they can be simulated using records.
Line 200 ⟶ 201:
 
As a bonus, default values for the parameters left unspecified can be either implicitly set to zero by the struct initializer, or supplied in a wrapper-macro definition. But this is not idiomatic C by any stretch.
 
=={{header|C sharp|C#}}==
 
{{works with|C sharp|C#|4.0}}
 
Named parameters were added in C# 4.0. The examples below demonstrate how named parameters and optional parameters are a single concept in the language.
 
<lang csharp>using System;
 
namespace NamedParams
{
class Program
{
static void AddWidget(string parent, float x = 0, float y = 0, string text = "Default")
{
Console.WriteLine("parent = {0}, x = {1}, y = {2}, text = {3}", parent, x, y, text);
}
 
static void Main(string[] args)
{
AddWidget("root", 320, 240, "First");
AddWidget("root", text: "Origin");
AddWidget("root", 500);
AddWidget("root", text: "Footer", y: 400);
}
}
}</lang>
 
Output:
 
<pre>parent = root, x = 320, y = 240, text = First
parent = root, x = 0, y = 0, text = Origin
parent = root, x = 500, y = 0, text = Default
parent = root, x = 0, y = 400, text = Footer</pre>
 
=={{header|C++}}==
Line 280 ⟶ 315:
function_with_named_parameters( _bar = 2.5, _bonk= "Hello", _foo = 9);
function_with_named_parameters(9, 2.5, true, "Hello");</lang>
 
=={{header|C sharp|C#}}==
 
{{works with|C sharp|C#|4.0}}
 
Named parameters were added in C# 4.0. The examples below demonstrate how named parameters and optional parameters are a single concept in the language.
 
<lang csharp>using System;
 
namespace NamedParams
{
class Program
{
static void AddWidget(string parent, float x = 0, float y = 0, string text = "Default")
{
Console.WriteLine("parent = {0}, x = {1}, y = {2}, text = {3}", parent, x, y, text);
}
 
static void Main(string[] args)
{
AddWidget("root", 320, 240, "First");
AddWidget("root", text: "Origin");
AddWidget("root", 500);
AddWidget("root", text: "Footer", y: 400);
}
}
}</lang>
 
Output:
 
<pre>parent = root, x = 320, y = 240, text = First
parent = root, x = 0, y = 0, text = Origin
parent = root, x = 500, y = 0, text = Default
parent = root, x = 0, y = 400, text = Footer</pre>
 
=={{header|Clojure}}==
Line 384 ⟶ 385:
<pre>x=12, y=3, z=24.4, dsc=Foo
x=12, y=42, z=12.2, dsc=Useless text</pre>
 
=={{header|E}}==
 
Since E supports arbitrary pattern matching (in the sense of [[Pattern Matching]] in parameter lists, a map-pattern can be used to provide named parameters, though the syntax is sufficiently noisy that this is not used casually.
 
<lang e>def printName([=> first := null, => last := null]) {
if (last == null) {
print("?")
} else {
print(last)
}
if (first != null) {
print(", ")
print(first)
}
}</lang>
 
(Note: In map literals and map patterns, “<code>=> <var>x</var></code>” is shorthand for “<code>"<var>x</var>" => <var>x</var></code>”.)
 
<lang e>? printName(["first" => "John"])
?, John
 
? printName(["last" => "Doe"])
Doe
 
? printName(["first" => "John", "last" => "Doe"])
Doe, John</lang>
 
=={{header|Elixir}}==
Line 416 ⟶ 444:
9 gustav
</pre>
 
=={{header|E}}==
 
Since E supports arbitrary pattern matching (in the sense of [[Pattern Matching]] in parameter lists, a map-pattern can be used to provide named parameters, though the syntax is sufficiently noisy that this is not used casually.
 
<lang e>def printName([=> first := null, => last := null]) {
if (last == null) {
print("?")
} else {
print(last)
}
if (first != null) {
print(", ")
print(first)
}
}</lang>
 
(Note: In map literals and map patterns, “<code>=> <var>x</var></code>” is shorthand for “<code>"<var>x</var>" => <var>x</var></code>”.)
 
<lang e>? printName(["first" => "John"])
?, John
 
? printName(["last" => "Doe"])
Doe
 
? printName(["first" => "John", "last" => "Doe"])
Doe, John</lang>
 
=={{header|Factor}}==
Line 707 ⟶ 708:
formatName({})
</lang>
 
=={{header|Julia}}==
Julia supports arbitrary named keyword arguments, which are listed (with their default values) after a <code>;</code> in the function definition:
Line 900 ⟶ 901:
param1: a1
param2: b2</pre>
 
 
=={{header|Modula-3}}==
Line 1,081:
</pre>
This is useful when you want your function to take both named (the hash) and positional (Joe Schmoe's $name) parameters.
 
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
Perl 6's support for optional parameters is much like Python's. Consider this declaration:
 
<lang perl6>sub funkshun ($a, $b?, $c = 15, :$d, *@e, *%f) {
...
}</lang>
 
In the above signature:
* <code>$a</code> is a mandatory parameter accepted by position (<code>funkshun 15, ...</code>).
* <code>$b</code> is an optional parameter that can be passed by position or by name. By default, it's undefined.
* <code>$c</code> is an optional parameter that can be passed by position or by name. Its default value is <code>15</code>.
* <code>$d</code> is an optional parameter that can only be passed by name. By default, it's undefined.
* <code>@e</code> is a slurpy array: it receives any leftover positional arguments.
* <code>%f</code> is a slurpy hash: it receives any leftover named arguments.
 
So, if we defined the function like this:
 
<lang perl6>sub funkshun ($a, $b?, :$c = 15, :$d, *@e, *%f) {
say "$a $b $c $d";
say join ' ', @e;
say join ' ', keys %f;
}
 
# this particularly thorny call:
 
funkshun
'Alfa', k1 => 'v1', c => 'Charlie', 'Bravo', 'e1',
d => 'Delta', 'e2', k2 => 'v2';</lang>
 
would print this:
 
<pre>Alfa Bravo Charlie Delta
e1 e2
k1 k2</pre>
 
=={{header|Phix}}==
Line 1,217 ⟶ 1,180:
PS> Greeting John
Hello John!</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>:- initialization(main).
 
main :-
sum(b=2,output=Output,a=1),
writeln(Output).
 
sum(A1,B1,C1) :-
named_args([A1,B1,C1],[a=A,b=B,output=Output]),
Output is A + B.
 
named_args([],_).
named_args([A|B],C) :-
member(A,C),
named_args(B,C).
</lang>
 
=={{header|Python}}==
Line 1,392 ⟶ 1,373:
SyntaxError: non-keyword arg after keyword arg
>>></lang>
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>:- initialization(main).
 
main :-
sum(b=2,output=Output,a=1),
writeln(Output).
 
sum(A1,B1,C1) :-
named_args([A1,B1,C1],[a=A,b=B,output=Output]),
Output is A + B.
 
named_args([],_).
named_args([A|B],C) :-
member(A,C),
named_args(B,C).
</lang>
 
=={{header|R}}==
Line 1,443 ⟶ 1,407:
(pizza #:topping "onion" "garlic" #:type "pan")
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
Perl 6's support for optional parameters is much like Python's. Consider this declaration:
 
<lang perl6>sub funkshun ($a, $b?, $c = 15, :$d, *@e, *%f) {
...
}</lang>
 
In the above signature:
* <code>$a</code> is a mandatory parameter accepted by position (<code>funkshun 15, ...</code>).
* <code>$b</code> is an optional parameter that can be passed by position or by name. By default, it's undefined.
* <code>$c</code> is an optional parameter that can be passed by position or by name. Its default value is <code>15</code>.
* <code>$d</code> is an optional parameter that can only be passed by name. By default, it's undefined.
* <code>@e</code> is a slurpy array: it receives any leftover positional arguments.
* <code>%f</code> is a slurpy hash: it receives any leftover named arguments.
 
So, if we defined the function like this:
 
<lang perl6>sub funkshun ($a, $b?, :$c = 15, :$d, *@e, *%f) {
say "$a $b $c $d";
say join ' ', @e;
say join ' ', keys %f;
}
 
# this particularly thorny call:
 
funkshun
'Alfa', k1 => 'v1', c => 'Charlie', 'Bravo', 'e1',
d => 'Delta', 'e2', k2 => 'v2';</lang>
 
would print this:
 
<pre>Alfa Bravo Charlie Delta
e1 e2
k1 k2</pre>
 
=={{header|REXX}}==
Line 1,695 ⟶ 1,697:
To process the argument list and call ''example'', use:
<lang sml>dosomething (args [A "tam", B 42.0]);</lang>
 
=={{header|Suneido}}==
Suneido can handle named and unnamed parameters. When using a combination, unnamed parameters must come before named ones and must be in the correct order. Named parameters can be in any order. Named parameters are given a default value so they are not mandatory.
Line 1,789 ⟶ 1,792:
'VBA expects a named parameter for 3
End Sub</lang>
 
=={{header|Visual Basic}}==
<lang vb>'the function
10,351

edits