Named parameter

Named parameter

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.

Contents

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 many languages: a non-exhaustive selection of examples would include Ada, C# 4.0+, Common Lisp, Scala, 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 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);

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


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Parameter (computer science) — In computer programming, a parameter is a variable which takes on the meaning of a corresponding argument passed in a call to a subroutine. In the most common case, call by value, a parameter acts within the subroutine as a local (isolated) copy… …   Wikipedia

  • Named and Shamed — Not to be confused with Name and Shame. Named and Shamed Studio album by The Flaming Stars Released 16 November 2004 …   Wikipedia

  • Immirzi parameter — The Immirzi parameter (also known as the Barbero Immirzi parameter) is a numerical coefficient appearing in loop quantum gravity, a nonperturbative theory of quantum gravity. The Immirzi parameter measures the size of the quantum of area in… …   Wikipedia

  • One-parameter group — In mathematics, a one parameter group or one parameter subgroup usually means a continuous group homomorphism φ : R → G from the real line R (as an additive group) to some other topological group G. That means that it is not in fact a group …   Wikipedia

  • Scientific phenomena named after people — This is a list of scientific phenomena and concepts named after people (eponymous phenomena). For other lists of eponyms, see eponym. NOTOC A* Abderhalden ninhydrin reaction Emil Abderhalden * Abney effect, Abney s law of additivity William de… …   Wikipedia

  • Stone's theorem on one-parameter unitary groups — In mathematics, Stone s theorem on one parameter unitary groups is a basic theorem of functional analysis which establishes a one to one correspondence between self adjoint operators on a Hilbert space H and one parameter families of unitary… …   Wikipedia

  • Peskin-Takeuchi parameter — In particle physics, the Peskin Takeuchi parameters are a set of three measurable quantities, called S , T , and U , that parameterize potential new physics contributions to electroweak radiative corrections. They are named after physicists… …   Wikipedia

  • Grüneisen Parameter — The Grüneisen Parameter, named after Eduard Grüneisen, describes the alteration in a crystal lattice s vibration frequency (phonon) based on the lattice s increase or decrease in volume as a result of temperature change.* See also: Debye model… …   Wikipedia

  • Help:Template — Wiki markup Basic markup Text, links, and talk pages Visual files Sound files Tables Template editing Using HTML …   Wikipedia

  • AS/400 Control Language — Infobox programming language name = AS/400 Control Language paradigm = imperative year = designer = IBM developer = IBM latest release version = latest release date = latest test version = latest test date = typing = implementations = dialects =… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”