Product Code Database
Example Keywords: the legend -apple $97
   » » Wiki: Named Parameter
Tag Wiki 'Named Parameter'.

In computer programming, named parameters, named-parameter arguments, named arguments or keyword arguments refer to a computer language's support for function calls to clearly associate each argument with a given parameter parameter within the function call.

A function call using named parameters differs from a regular function call in that the arguments are passed by associating each one with a parameter name, instead of providing an ordered list of arguments.

For example, consider this Java or C# method call that doesn't use named parameters:

window.addNewControl("Title", 20, 50, 100, 50, true);

Using named parameters in Python, the call can be written as: window.addNewControl(title="Title",

Using named parameters in , the call can be written as: $window->addNewControl(title: "Title",
                      xPosition: 20,
                      yPosition: 50,
                      width: 100,
                      height: 50,
                      drawingNow: True);
The version with positional arguments is more implicit. The versions that name parameters are more explicit. Depending on circumstance, a programmer may find one or the other to be 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), , , IDL, Kotlin, , PL/SQL, , Python, R, , Ruby, Scala, , Swift and . does not have named parameters (even though parts of the method name may look like named parameters). Developer Library - The Implementation of a Class Provides Its Internal Behavior
In C++, you can achieve named parameters by using designated initializers since C++20, like so: struct A {int a{}, int b{} };

void foo(A bar) {

  std::cout << bar.a << bar.b;
} foo({ .a = 1, .b = 3 });

Order of parameters
In languages that do not support named parameters, the order of arguments in a function call is necessarily fixed, since it is the only way that the language can identify which argument is intended to be used for which parameter.

With named parameters, it is usually possible to provide the arguments in any order, since the parameter name attached to each argument identifies its purpose. This reduces the between parts of the program. A few languages support named parameters but still require the arguments to be provided in a specific order.

Optional parameters and positional 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 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 argument after all optional named-parameter arguments: 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 .

In , the codes (variables) <nowiki></nowiki>, <nowiki></nowiki> in templates and so on, will be replaced by the first, second, and so on unnamed parameter (or the value of a parameter named 1, 2, etc.); these are known as ''s.

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 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? */

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 or associative array.

For example, in , 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: 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 };

Special Support
In and pre-2.0 Ruby a similar convention exists (generally called a hash or options hash Programming Perl 2.9: Hashes), 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. Perl core module Net::FTP

With chained method calls
In object-oriented programming languages, it is possible to use to simulate named parameters, as a form of . 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.C++ FAQ, 10.20 What is the "Named Parameter Idiom"? The object may then be passed to a function that uses the arguments it contains.

is often used in conjunction with the as a way to override default values provided by the builder class.

See also

External links

Page 1 of 1
Page 1 of 1


Pages:  ..   .. 
Items:  .. 


General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 


Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time