Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Parameter (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Argument passing == The exact mechanism for assigning arguments to parameters, called ''argument passing'', depends upon the [[evaluation strategy]] used for that parameter (typically [[call by value]]), which may be specified using keywords. === Default arguments === Some programming languages such as [[Ada (programming language)|Ada]], [[C++]], [[Clojure]],{{Citation needed|date=June 2021}} [[Common Lisp]],<ref>{{Cite web|title=Functions|url=https://gigamonkeys.com/book/functions.html|access-date=2021-06-02|website=gigamonkeys.com}}</ref> [[Fortran 90]],<ref>{{Cite web|title=optional arguments|url=http://www.netlib.org/ccm/page/api/optional.html|access-date=2021-06-02|website=www.netlib.org}}</ref> [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[Tcl (programming language)|Tcl]], and [[Windows PowerShell]]{{Citation needed|date=June 2021}} allow for a [[default argument]] to be explicitly or implicitly given in a subroutine's declaration. This allows the caller to omit that argument when calling the subroutine. If the default argument is explicitly given, then that value is used if it is not provided by the caller. If the default argument is implicit (sometimes by using a keyword such as ''Optional'') then the language provides a well-known value (such as ''[[Null pointer|null]]'', ''Empty'', zero, an empty string, etc.) if a value is not provided by the caller. PowerShell example: <syntaxhighlight lang="powershell"> function doc($g = 1.21) { "$g gigawatts? $g gigawatts? Great Scott!" } </syntaxhighlight> <syntaxhighlight lang="ps1con"> PS > doc 1.21 gigawatts? 1.21 gigawatts? Great Scott! PS > doc 88 88 gigawatts? 88 gigawatts? Great Scott! </syntaxhighlight> Default arguments can be seen as a special case of the variable-length argument list. === Variable-length parameter lists === Some languages allow subroutines to be defined to accept a [[Variadic function|variable number of arguments]]. For such languages, the subroutines must iterate through the list of arguments. PowerShell example: <syntaxhighlight lang="powershell"> function marty { $args | foreach { "back to the year $_" } } </syntaxhighlight> <syntaxhighlight lang="ps1con"> PS > marty 1985 back to the year 1985 PS > marty 2015 1985 1955 back to the year 2015 back to the year 1985 back to the year 1955 </syntaxhighlight> === Named parameters === Some programming languages—such as [[Ada (programming language)|Ada]] and [[Windows PowerShell]]—allow subroutines to have [[named parameter]]s. This allows the calling code to be more [[self-documenting]]. It also provides more flexibility to the caller, often allowing the order of the arguments to be changed, or for arguments to be omitted as needed. PowerShell example: <syntaxhighlight lang="powershell"> function jennifer($adjectiveYoung, $adjectiveOld) { "Young Jennifer: I'm $adjectiveYoung!" "Old Jennifer: I'm $adjectiveOld!" } </syntaxhighlight> <syntaxhighlight lang="ps1con"> PS > jennifer 'fresh' 'experienced' Young Jennifer: I'm fresh! Old Jennifer: I'm experienced! PS > jennifer -adjectiveOld 'experienced' -adjectiveYoung 'fresh' Young Jennifer: I'm fresh! Old Jennifer: I'm experienced! </syntaxhighlight> === Multiple parameters in functional languages=== In [[lambda calculus]], each function has exactly one parameter. What is thought of as functions with multiple parameters is usually represented in lambda calculus as a function which takes the first argument, and returns a function which takes the rest of the arguments; this is a transformation known as [[currying]]. Some programming languages, like [[ML (programming language)|ML]] and [[Haskell (programming language)|Haskell]], follow this scheme. In these languages, every function has exactly one parameter, and what may look like the definition of a function of multiple parameters, is actually [[syntactic sugar]] for the definition of a function that returns a function, etc. [[Function application]] is [[Operator associativity|left-associative]] in these languages as well as in lambda calculus, so what looks like an application of a function to multiple arguments is correctly evaluated as the function applied to the first argument, then the resulting function applied to the second argument, etc.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)