Named parameter
In computer programming, named parameters refers 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. Also, depending on the specific language used, using named parameters may allow you to change the order in which you list them. (e.g.: Ada)
Usage in Languages
Named parameters are not used in languages like Java and C/C++. They are supported in languages like Ada, PL/SQL, Smalltalk, Common Lisp and Objective C. In Objective Caml, named parameters are called labels.
Emulating
One may be able to achieve a similar effect in other languages (such as C) that do not have named parameters, by using either comments or a data structure:
With comments (in C):
#define TRUE -1 MyFunctionCall( 20, /* x coordinate */ 50, /* y coordinate */ 100, /* width */ 5, /* height */ TRUE /* drawing now? */ );
Note that if using comments as above, the order of the arguments becomes important.
With a data structure (in C):
#define TRUE -1 struct MyStruct{ int XCoordinate, int YCoordinate, int Width, int Height, unsigned char drawingNow}; . . . MyStruct StructParameter; StructParameter.XCoordinate = 20; StructParameter.YCoordinate = 50; StructParameter.Width = 100; StructParameter.Height = 5; StructParameter.drawingNow = TRUE; MyFunctionCall( StructParameter );
Notes
A good IDE provides the programmer with the same information in a much more concise form through the use of tooltips and lists. For example, Eclipse does this for Java. The same applies to other languages that have that kind of IDE.
Note that named parameters in some languages (such as Smalltalk and Objective-C) refers to the syntactical presentation and not to the underlying implementation. Neither language supports named parameters in the sense that Python supports key=value parameters. For example, in the above Objective-C fragment, the method name is literally "addNewControlWithTitle:xPosition:yPosition:width:height:drawingNow:".