Named parameter: Difference between revisions
→Use in programming languages: c# => c# 4.0+ |
Clean up some cruft, and explain a couple more use cases. Still no references though :v |
||
Line 21: | Line 21: | ||
</source> |
</source> |
||
The Objective-C version is more explicit, while the Java version is more concise. Depending on the particular instance, a programmer may find one or the other easier to read. |
The Objective-C version is more explicit, while the Java version is more concise. Depending on the particular instance, a programmer may find one or the other easier to read. |
||
== Use in programming languages == |
== Use in programming languages == |
||
Named parameters are |
Named parameters are supported in too many languages to list: a non-exhaustive selection of examples would include [[Ada (programming language)|Ada]], [[C Sharp 4.0|C# 4.0+]], [[Common Lisp]], [[Mathematica]], [[PL/SQL]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Smalltalk]], and [[Visual Basic]]. |
||
Languages which notably do not support them include [[C (programming language)|C]], [[C++]], and [[Java (programming language)|Java]]. |
|||
== Order of parameters == |
|||
In languages without named parameters, the order of parameters is necessarily fixed, since it is the only thing that the language can use to identify which value is intended to be used for which purpose. |
|||
With named parameters, it is usually possible to provide the values in any arbitrary order, since the name attached to each value identifies its purpose. A few languages such as [[Objective-C]] use names but require the parameters to be provided in a specific order. |
|||
== Optional parameters == |
|||
Named parameters are often used in conjunction with optional parameters. Without named parameters, optional parameters can only appear at the end of the parameter list, since there is no other way to determine which values have been omitted. In languages that support named optional parameters, however, the programmer may supply any subset of the available parameters, and the names are used to determine which values have been provided. |
|||
An additional complication arises in languages such as [[Objective Caml]] that support both optional named parameters and [[partial application]]: it is impossible in general to distinguish between a partially-applied function and a function to which a subset of parameters have been provided. OCaml resolves this ambiguity by requiring a positional parameter to follow all optional named parameters: its presence or absence is used to decide whether the function has been fully or partially applied. |
|||
== Emulating == |
== Emulating == |
||
In languages without named parameters, some of the same benefits can be achieved in other ways. |
|||
=== With documentation === |
|||
With comments (in [[C (programming language)|C]]): |
|||
Their value as documentation can be replicated by tooltips in IDEs for languages such as [[Java]], or with comments (in [[C (programming language)|C]]): |
|||
<source lang="c"> |
<source lang="c"> |
||
MyFunctionCall( |
MyFunctionCall( |
||
Line 40: | Line 55: | ||
</source> |
</source> |
||
But this does not provide any checking, and the order of arguments remains important. |
|||
=== With data structures === |
|||
The removal of 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]], the following two calls are equivalent: |
|||
<source lang="JavaScript"> |
<source lang="JavaScript"> |
||
MyFunctionCall({ xPosition: 20, yPosition: 50, width: 100, height: 5, |
MyFunctionCall({ xPosition: 20, yPosition: 50, width: 100, height: 5, |
||
Line 72: | Line 91: | ||
</source> |
</source> |
||
=== With chained method calls === |
|||
== Notes == |
|||
Some [[Integrated development environment|IDEs]] provide the programmer with the same information in a much more concise form through the use of tooltips and lists. For example, [[Eclipse (software)|Eclipse]] does this for Java. The same applies to other languages that have that kind of IDE. |
|||
In object-oriented languages, it is possible to use [[method chaining]] to simulate named parameters. Each named parameter is replaced with a method on a parameter object that modifies and then returns the object. The object may then be passed to a function that uses the parameters it contains.[http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20] |
|||
Note that '''named parameters''' in some languages (such as Smalltalk and Objective-C) refers to the syntactic presentation and not to the underlying implementation. Neither language supports named parameters in the sense that [[Python (programming language)|Python]] supports ''key=value'' parameters. For example, in the above Objective-C fragment, the method name is literally "addNewControlWithTitle:xPosition:yPosition:width:height:drawingNow:". |
|||
==See also== |
==See also== |
Revision as of 01:57, 26 May 2011
In computer programming, named parameters or keyword arguments refer to a computer language's support for function calls that clearly state the name of each parameter within the function call itself.
Overview
A function call using named parameters differs from a regular function call in that the values are passed by associating each one with a parameter name, instead of providing an ordered list of values.
For example, consider the following Java method call that does not use named parameters:
window.addNewControl("Title", 20, 50, 100, 50, true);
Using named parameters in Objective-C, the call can be written as:
[window addNewControlWithTitle:@"Title"
xPosition:20
yPosition:50
width:100
height:50
drawingNow:YES];
The Objective-C version is more explicit, while the Java version is more concise. Depending on the particular instance, a programmer may find one or the other easier to read.
Use in programming languages
Named parameters are supported in too many languages to list: a non-exhaustive selection of examples would include Ada, C# 4.0+, Common Lisp, Mathematica, PL/SQL, Python, R, Smalltalk, and Visual Basic.
Languages which notably do not support them include C, C++, and Java.
Order of parameters
In languages without named parameters, the order of parameters is necessarily fixed, since it is the only thing that the language can use to identify which value is intended to be used for which purpose.
With named parameters, it is usually possible to provide the values in any arbitrary order, since the name attached to each value identifies its purpose. A few languages such as Objective-C use names but require the parameters to be provided in a specific order.
Optional parameters
Named parameters are often used in conjunction with optional parameters. Without named parameters, optional parameters can only appear at the end of the parameter list, since there is no other way to determine which values have been omitted. In languages that support named optional parameters, however, the programmer may supply any subset of the available parameters, and the names are used to determine which values have been provided.
An additional complication arises in languages such as Objective Caml that support both optional named parameters and partial application: it is impossible in general to distinguish between a partially-applied function and a function to which a subset of parameters have been provided. OCaml resolves this ambiguity by requiring a positional parameter to follow all optional named parameters: its presence or absence is used to decide whether the function has been fully or partially applied.
Emulating
In languages without 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 IDEs for languages such as Java, or with comments (in C):
MyFunctionCall(
20, /* x coordinate */
50, /* y coordinate */
100, /* width */
5, /* height */
TRUE /* drawing now? */
);
But this does not provide any checking, and the order of arguments remains important.
With data structures
The removal of the argument order restriction, and the ability to leave some values unspecified, can be achieved by passing a record or associative array.
For example, in JavaScript, the following two calls are equivalent:
MyFunctionCall({ xPosition: 20, yPosition: 50, width: 100, height: 5,
drawingNow: true });
MyFunctionCall({ width: 100, height: 5, xPosition: 20, yPosition: 50,
drawingNow: true });
Compare to C:
struct MyStruct
{
int XCoordinate;
int YCoordinate;
int Width;
int Height;
unsigned char drawingNow;
};
…
MyStruct parameters;
parameters.XCoordinate = 20;
parameters.YCoordinate = 50;
parameters.Width = 100;
parameters.Height = 5;
parameters.drawingNow = TRUE;
MyFunctionCall(¶meters);
With chained method calls
In object-oriented languages, it is possible to use method chaining to simulate named parameters. Each named parameter is replaced with a method on a parameter object that modifies and then returns the object. The object may then be passed to a function that uses the parameters it contains.[1]
See also
External links
- http://web.archive.org/web/20070502112455/http://plg.uwaterloo.ca/~rgesteve/cforall/named_pars.html
- In C++ this paradigm can be easily implemented: Boost Parameter Library
- Named Parameters in various programming languages at Rosetta Code