Jump to content

Named parameter: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Small WP:EoS WP:COPYEDITs. WP:LINKs: updates, adds, fix-cut WP:NOPIPEs. Full term define before WP:ABBReviation. Nonlead-word nonproper noun MOS:CAPS > low-case.
Line 1: Line 1:
{{refimprove|date=August 2014}}
{{refimprove|date=August 2014}}


In [[computer programming]], '''named parameters''', '''pass-by-name''', or '''keyword arguments''' refer to a computer language's support for function calls that clearly state the name of each [[Parameter (computer science)|parameter]] within the function call itself.
In [[computer programming]], '''named parameters''', '''pass-by-name''', or '''keyword arguments''' refer to a computer language's support for function calls that clearly state the name of each [[Parameter (computer science)|parameter]] within the function call.


== Overview ==
== 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.
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 (programming language)|Java]] method call that does not use named parameters:
For example, consider this [[Java (programming language)|Java]] method call using no named parameters:
<source lang="java">
<source lang="java">
window.addNewControl("Title", 20, 50, 100, 50, true);
window.addNewControl("Title", 20, 50, 100, 50, true);
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 Java version is more concise. The Objective-C version is more explicit. Depending on a given instance, a programmer may find one or the other easier to read.


== Use in programming languages ==
== Use in programming languages ==
Named parameters are explicitly supported in many languages: a non-exhaustive selection of examples would include [[Ada (programming language)|Ada]], [[C Sharp 4.0|C# 4.0+]], [[Ceylon (programming language)|Ceylon]], [[Adobe ColdFusion|Coldfusion]], [[Common Lisp]], [[Scala (programming language)|Scala]], [[Kotlin (programming language)|Kotlin]], [[Mathematica]], [[PL/SQL]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], [[Smalltalk]], [[Fortran]], [[Visual Basic]], [[IDL (programming language)|IDL]], [[Swift (Apple programming language)|Swift]] and [[Objective-C|Objective C]].
Named parameters are supported explicitly in many languages: a non-exhaustive selection of examples includes [[Ada (programming language)|Ada]], [[C Sharp 4.0|C# 4.0+]], [[Ceylon (programming language)|Ceylon]], [[ColdFusion Markup Language]] (CFML), [[Common Lisp]], [[Scala (programming language)|Scala]], [[Kotlin (programming language)|Kotlin]], [[Mathematica]], [[PL/SQL]], [[Python (programming language)|Python]], [[R (programming language)|R]], [[Ruby (programming language)|Ruby]], [[Smalltalk]], [[Fortran]], [[Visual Basic]], [[IDL (programming language)|IDL]], [[Swift (programming language)|Swift]], and [[Objective-C]].


== Order of parameters ==
== Order of parameters ==


In languages without named parameters, the ''order'' of parameters in a function call is necessarily fixed, since it is the only way that the language can identify which value is intended to be used for which purpose.
In languages with no named parameters, the ''order'' of parameters in a function call is necessarily fixed, since it is the only way that the language can 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. This reduces the [[connascence]] between parts of the program. A few languages such as [[Objective-C]] use named parameters but also require the parameters to be provided in a specific order.
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. This reduces the [[connascence]] between parts of the program. A few languages such as [[Objective-C]] use named parameters and require the parameters to be provided in a specific order.


== Optional parameters ==
== 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.
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, programs 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 [[OCaml]] 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. If all parameters are optional, the implementor may solve the issue by adding a dummy positional parameter of type [[Unit type|unit]].
An added complication arises in languages such as [[OCaml]] that support both optional named parameters and [[partial application]]. It is impossible in general to distinguish between a function partly applied, and a function to which a subset of parameters have been provided. OCaml resolves this ambiguity by requiring a positional parameter after all optional named parameters: its presence or absence is used to decide if the function has been fully or partly applied. If all parameters are optional, the implementor may solve the issue by adding a dummy positional parameter of type [[Unit type|unit]].


== Emulating ==
== Emulating ==
Line 42: Line 42:


=== With documentation ===
=== With documentation ===
Their value as documentation can be replicated by tooltips in IDEs for languages such as [[Java (programming language)|Java]], or with comments (in [[C (programming language)|C]]):
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]]):
<source lang="c">
<source lang="c">
MyFunctionCall(
MyFunctionCall(
Line 57: Line 57:
=== With data structures ===
=== 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]].
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]], the following two calls are equivalent:
For example, in [[JavaScript]], these 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 84: Line 84:
</source>
</source>


In Perl, there is a similar convention. Functions with long parameter lists often accept an associative array (called a ''hash'' in Perl) that contains some or all parameters
In [[Perl]], there is a similar convention. Functions with long parameter lists often accept an associative array (called a ''hash'' in Perl) that contains some or all parameters
<ref>Perl Design Patterns TinyWiki, [http://perldesignpatterns.com/?NamedArguments NamedArguments]</ref>
<ref>Perl Design Patterns TinyWiki, [http://perldesignpatterns.com/?NamedArguments NamedArguments]</ref>
.<ref>As an example, the core module [http://perldoc.perl.org/Net/FTP.html Net::FTP]'s "new" function accepts a hash of optional arguments.</ref>
.<ref>As an example, the core module [http://perldoc.perl.org/Net/FTP.html Net::FTP]'s "new" function accepts a hash of optional arguments.</ref>
Line 90: Line 90:
=== With chained method calls ===
=== With chained method calls ===


In object-oriented languages, it is possible to use [[method chaining]] to simulate named parameters, as a form of [[fluent interface]]. Each named parameter is replaced with a method on a parameter object that modifies and then returns the object. In C++, this is known as 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>
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 is replaced with a method on a parameter 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 parameters it contains.
The object may then be passed to a function that uses the parameters it contains.
When this approach is extended to handle optional named parameters with default values,
When this method is extended to handle optional named parameters with default values,
it is known as the [[Builder pattern]].
it is termed the [[builder pattern]].


==See also==
==See also==
* [[Tag (programming)|Tag]]
* [[Tag (programming)]]


==References==
==References==
{{reflist}}
{{Reflist}}


==External links==
==External links==
* http://web.archive.org/web/20070502112455/http://plg.uwaterloo.ca/~rgesteve/cforall/named_pars.html
* http://web.archive.org/web/20070502112455/http://plg.uwaterloo.ca/~rgesteve/cforall/named_pars.html
* In C++ this paradigm can be easily implemented: [http://www.boost.org/doc/libs/1_53_0/libs/parameter/doc/html/index.html Boost Parameter Library]
* In C++ this paradigm can be easily implemented: [http://www.boost.org/doc/libs/1_53_0/libs/parameter/doc/html/ Boost Parameter Library]
* [http://rosettacode.org/wiki/Named_parameters Named Parameters in various programming languages] at [[Rosetta Code]]
* [http://rosettacode.org/wiki/Named_parameters Named Parameters in various programming languages] at [[Rosetta Code]]



Revision as of 05:40, 9 June 2016

In computer programming, named parameters, pass-by-name, 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.

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 this Java method call using no 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 Java version is more concise. The Objective-C version is more explicit. Depending on a given instance, a programmer may find one or the other easier to read.

Use in programming languages

Named parameters are supported explicitly in many languages: a non-exhaustive selection of examples includes Ada, C# 4.0+, Ceylon, ColdFusion Markup Language (CFML), Common Lisp, Scala, Kotlin, Mathematica, PL/SQL, Python, R, Ruby, Smalltalk, Fortran, Visual Basic, IDL, Swift, and Objective-C.

Order of parameters

In languages with no named parameters, the order of parameters in a function call is necessarily fixed, since it is the only way that the language can 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. This reduces the connascence between parts of the program. A few languages such as Objective-C use named parameters and 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, programs may supply any subset of the available parameters, and the names are used to determine which values have been provided.

An added complication arises in languages such as OCaml that support both optional named parameters and partial application. It is impossible in general to distinguish between a function partly applied, and a function to which a subset of parameters have been provided. OCaml resolves this ambiguity by requiring a positional parameter after all optional named parameters: its presence or absence is used to decide if the function has been fully or partly applied. If all parameters are optional, the implementor may solve the issue by adding a dummy positional parameter of type unit.

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 integrated development environments (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

Removing 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, these 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 C99:[1]

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(&parameters);

In Perl, there is a similar convention. Functions with long parameter lists often accept an associative array (called a hash in Perl) that contains some or all parameters [2] .[3]

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 is replaced with a method on a parameter object that modifies and then returns the object. In C++, this is termed the named parameter idiom.[4] The object may then be passed to a function that uses the parameters it contains. When this method is extended to handle optional named parameters with default values, it is termed the builder pattern.

See also

References

  1. ^ https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
  2. ^ Perl Design Patterns TinyWiki, NamedArguments
  3. ^ As an example, the core module Net::FTP's "new" function accepts a hash of optional arguments.
  4. ^ C++ FAQ, 10.20 What is the "Named Parameter Idiom"?