unnamed parameter
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, named parameters, named argument or keyword arguments refer to a computer language's support for function calls that clearly state the name of each
parameter A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
within the function call.


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 this
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
or C# method call using no named parameters: window.addNewControl("Title", 20, 50, 100, 50, true); Using named parameters in
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
, the call can be written as: window.addNewControl(title="Title", xPosition=20, yPosition=50, width=100, height=50, drawingNow=True) Using named parameters in
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
, 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 with named arguments is more explicit. Depending on a given instance, a programmer may find one or the other easier to read.


Use in programming languages

Named parameters are supported explicitly in many languages. A non-exhaustive selection of examples includes
Ada Ada may refer to: Places Africa * Ada Foah, a town in Ghana * Ada (Ghana parliament constituency) * Ada, Osun, a town in Nigeria Asia * Ada, Urmia, a village in West Azerbaijan Province, Iran * Ada, Karaman, a village in Karaman Province, ...
, C# 4.0+, Ceylon,
ColdFusion Markup Language ColdFusion Markup Language, more commonly known as CFML, is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine. Multiple commercial and open source implementations of CFML engines are availab ...
(CFML), Common Lisp, Fortran, IDL, Kotlin, Mathematica,
PL/SQL PL/SQL (Procedural Language for SQL) is Oracle Corporation's procedural extension for SQL and the Oracle relational database. PL/SQL is available in Oracle Database (since version 6 - stored PL/SQL procedures/functions/packages/triggers since ...
,
PowerShell PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Initially a Windows component only, known as Windows PowerShell, it was made open-sou ...
,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
, R,
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, Scala, Smalltalk,
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIFT, ...
and
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic ( ...
. Note that
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXT ...
does not have named parameters (even though parts of the method name may look like named parameters).


Order of parameters

In languages with no named parameters, the ''order'' of parameters in a function call is necessarily fixed, since it is the only way that the language can 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. This reduces the
connascence Connascence () is a software quality metric invented by Meilir Page-Jones to allow reasoning about the complexity caused by dependency relationships in object-oriented design much like coupling did for structured design. In software engineering, ...
between parts of the program. A few languages use named parameters but still require the parameters 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 OCaml that support both optional named parameters and
partial application In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function f \colon (X \times Y \times Z) \to N , ...
. 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 parameter after all optional named parameters: 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
unit Unit may refer to: Arts and entertainment * UNIT, a fictional military organization in the science fiction television series ''Doctor Who'' * Unit of action, a discrete piece of action (or beat) in a theatrical presentation Music * ''Unit'' (a ...
. In
MediaWiki MediaWiki is a free and open-source wiki software. It is used on Wikipedia and almost all other Wikimedia websites, including Wiktionary, Wikimedia Commons and Wikidata; these sites define a large part of the requirement set for MediaWi ...
, the codes (variables) , 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.


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
integrated development environment An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools ...
s (IDEs) for languages such as
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
, or with comments (in C): MyFunctionCall( 20, /* x coordinate */ 50, /* y coordinate */ 100, /* width */ 5, /* height */ TRUE /* drawing now? */ ); But this provides no checking, 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 In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms an ...
. For example, in
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, these two calls are equivalent: MyFunctionCall(); MyFunctionCall(); Compare to C99: struct MyParam ; … MyParam parameters = ; MyFunctionCall(¶meters);


Special Support

In
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
and pre-2.0
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
a similar convention exists (generally called a ''hash'' or ''options hash''), 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.


With chained method calls

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
languages, it is possible to use method chaining to simulate named parameters, as a form of
fluent interface In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
. Each named parameter is replaced with a method on a parameter 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"?
/ref> The object may then be passed to a function that uses the parameters it contains. Method chaining is often used in conjunction with the
builder pattern The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from it ...
as a way to override default values provided by the builder class.


See also

* Help:Template for named and positional parameters. *
Fluent interface In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric ...
* Tag (programming)


References

{{Reflist


External links

* https://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 Code Subroutines Articles with example code