Jump to content

Named parameter

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Short Circuit (talk | contribs) at 18:36, 27 July 2010 (External links: There's a wiki page for Rosetta Code on Wikipedia, so move part of link to intrawiki). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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. 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)

Use in programming languages

Named parameters are not used in languages like C, C++, and Java. They are supported in languages like Ada, C#, Common Lisp, Fortran, Mathematica, Objective-C, PL/SQL, Perl, Python, R, Smalltalk, Visual Basic. In Objective Caml, named parameters are called labels; labeled parameters can be made optional.

Emulating

Effects similar to named parameters can be achieved in languages without named parameters, by using either comments or a data structure:

With comments (in C):

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 is important.

In languages where throwaway objects can be created with relative ease, a data structure can be used, and argument order becomes irrelevant. 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(&parameters);

Notes

Some IDEs provide 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 syntactic 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:".

See also