Named parameter
Encyclopedia
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.
For example, consider the following Java
method call that does not use named parameters:
Using named parameters in Objective-C
, the call can be written as:
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.
, 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
.
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.
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.
, or with comments (in C
):
But this does not provide any checking, and the order of arguments remains important.
or associative array
.
For example, in JavaScript
, the following two calls are equivalent:
Compare to C:
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.http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, named parameters or keyword arguments refer to a computer language's support for function calls that clearly state the name of each parameter
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...
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
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
method call that does not use named parameters:
Using named parameters in Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...
, the call can be written as:
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 AdaAda (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
, C# 4.0+
C Sharp 4.0
C# 4.0 is the latest version of the C# programming language, which was released on April 11, 2010. Microsoft has released the 4.0 runtime and development environment Visual Studio 2010...
, Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
, Scala, Mathematica
Mathematica
Mathematica is a computational software program used in scientific, engineering, and mathematical fields and other areas of technical computing...
, PL/SQL
PL/SQL
PL/SQL is Oracle Corporation's procedural extension language for SQL and the Oracle relational database...
, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, R
R (programming language)
R is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....
, Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
, and Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
.
Languages which notably do not support them include C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, and Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
.
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
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...
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
Objective Caml
OCaml , originally known as Objective Caml, is the main implementation of the Caml programming language, created by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy and others in 1996...
that support both optional named parameters and partial application
Partial application
In computer science, partial application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity...
: 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 JavaJava
Java is an island of Indonesia. With a population of 135 million , it is the world's most populous island, and one of the most densely populated regions in the world. It is home to 60% of Indonesia's population. The Indonesian capital city, Jakarta, is in west Java...
, or with comments (in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
):
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 recordRecord (computer science)
In computer science, a record is an instance of a product of primitive data types called a tuple. In C it is the compound data in a struct. Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed...
or associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....
.
For example, in JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
, the following two calls are equivalent:
Compare to C:
With chained method calls
In object-oriented languages, it is possible to use method chainingMethod chaining
Method chaining is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object , allowing the calls to be chained together in a single statement...
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.http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20
External links
- http://web.archive.org/web/20070502112455/http://plg.uwaterloo.ca/~rgesteve/cforall/named_pars.html
- In C++ this paradigm can be easily implemented: Boost Parameter Library
- Named Parameters in various programming languages at Rosetta CodeRosetta CodeRosetta Code is a wiki-based programming chrestomathy website with solutions to various programming problems in many different programming languages. It was created in 2007 by Mike Mol. Rosetta Code includes 450 programming tasks, and covers 351 programming languages...