Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Named parameter
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Emulating == In languages that do not support named parameters, some of the same benefits can be achieved in other ways. === With documentation === Their value as documentation can be replicated by tooltips in [[integrated development environment]]s (IDEs) for languages such as [[Java (programming language)|Java]], or with comments (in [[C (programming language)|C]]): <syntaxhighlight lang="c"> MyFunctionCall( 20, /* x coordinate */ 50, /* y coordinate */ 100, /* width */ 5, /* height */ TRUE /* drawing now? */ ); </syntaxhighlight> Such comments are not checked for correctness and the order of arguments remains important. === With data structures === Removing the argument order restriction, and the ability to leave some values unspecified, can be achieved by passing a [[record (computer science)|record]] or [[associative array]]. For example, in [[JavaScript]], these two calls are equivalent: <syntaxhighlight lang="JavaScript"> MyFunctionCall({ xPosition: 20, yPosition: 50, width: 100, height: 5, drawingNow: true }); </syntaxhighlight> <syntaxhighlight lang="JavaScript"> MyFunctionCall({ width: 100, height: 5, xPosition: 20, yPosition: 50, drawingNow: true }); </syntaxhighlight> Compare to C99:<ref>{{Cite web|url=https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html|title = Designated Inits (Using the GNU Compiler Collection (GCC))}}</ref> <syntaxhighlight lang="c"> struct MyParam { int xPosition; int yPosition; int width; int height; unsigned char drawingNow; }; β¦ MyParam parameters = { .xPosition = 20, .yPosition = 50, .width = 100, .height = 5, .drawingNow = TRUE }; MyFunctionCall(¶meters); </syntaxhighlight> ==== Special Support ==== In [[Perl]] and pre-2.0 [[Ruby (programming language)|Ruby]] a similar convention exists (generally called a ''hash'' or ''options hash''<ref>[https://docstore.mik.ua/orelly/perl/prog3/ch02_09.htm Programming Perl 2.9: Hashes]</ref>), with special support for omitting the delimiters within function calls. As an example, the core module's Net::FTP ''new'' function accepts a hash of optional arguments.<ref>[http://perldoc.perl.org/Net/FTP.html Perl core module Net::FTP]</ref> === With chained method calls === In [[object-oriented programming]] languages, it is possible to use [[method chaining]] to simulate named parameters, as a form of [[fluent interface]]. Each named-parameter argument is replaced with a method on an "arguments" object that modifies and then returns the object. In C++, this is termed the ''named parameter idiom''.<ref>C++ FAQ, [http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20 10.20 What is the "Named Parameter Idiom"?]</ref> The object may then be passed to a function that uses the arguments it contains. [[Method chaining]] is often used in conjunction with the [[builder pattern]] as a way to override default values provided by the builder class.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)