Content deleted Content added
Stevebroshar (talk | contribs) →Example: Remove fluff |
Stevebroshar (talk | contribs) →Argument passing: exact adds no value |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 1:
{{Short description |
{{Other uses |Parameter (disambiguation)}}
In [[computer programming]], a '''parameter''', a.k.a. '''formal argument''', is a [[Variable (computer science)|variable]] that represents an argument, a.k.a. actual argument, a.k.a. actual parameter, to a [[
For example, consider
Parameter passing is defined by a programming language. [[Evaluation strategy]] defines the semantics for how parameters can be declared and how arguments are passed to a
==Example==
The following
<syntaxhighlight lang="c">
Line 16:
}
</syntaxhighlight>
== Parameters and arguments ==
Line 28 ⟶ 26:
By contrast, the arguments are the expressions<ref>{{Cite web|url=http://crasseux.com/books/ctutorial/Actual-parameters-and-formal-parameters.html|title=The GNU C Programming Tutorial|website=crasseux.com|language=en|access-date=2018-10-27}}</ref> supplied to the procedure when it is called, usually one expression matching one of the parameters. Unlike the parameters, which form an unchanging part of the procedure's definition, the arguments may vary from call to call. Each time a procedure is called, the part of the procedure call that specifies the arguments is called the ''argument list''.
Although parameters are also commonly referred to as arguments, arguments are sometimes thought of as the actual values or references assigned to the parameter variables when the
Consider the following [[C (programming language)|C]] function, ''Sum'', which has two parameters, ''addend1'' and ''addend2''. It adds the values passed into the parameters, and returns the result to the function's caller.
<syntaxhighlight lang="c">
int Sum(int addend1, int addend2)
Line 47 ⟶ 37:
</syntaxhighlight>
The
<syntaxhighlight lang="c">
int value1 = 40;
Line 55 ⟶ 44:
int sum_value = Sum(value1, value2);
</syntaxhighlight>
Because of the difference between parameters and arguments, it is possible to supply inappropriate arguments to a procedure. The call may supply too many or too few arguments; one or more of the arguments may be a wrong type; or arguments may be supplied in the wrong order. Any of these situations causes a mismatch between the parameter and argument lists, and the procedure will often return an unintended answer or generate a [[runtime error]].
Line 99 ⟶ 85:
In [[strongly typed programming language]]s, each parameter's [[Datatype|type]] must be specified in the procedure declaration. Languages using [[type inference]] attempt to discover the types automatically from the function's body and usage. Dynamically typed programming languages defer type resolution until run-time. Weakly typed languages perform little to no type resolution, relying instead on the programmer for correctness.
Some languages use a special keyword (e.g. ''void'') to indicate that the
== Argument passing ==
The
=== 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
PowerShell example:
Line 123 ⟶ 109:
=== Variable-length parameter lists ===
Some languages allow
PowerShell example:
Line 142 ⟶ 128:
=== Named parameters ===
Some programming languages—such as [[Ada (programming language)|Ada]] and [[Windows PowerShell]]—allow
PowerShell example:
Line 263 ⟶ 249:
The micro-optimization of not requiring a local variable and copying the return when using output variables can also be applied to conventional functions and return values by sufficiently sophisticated compilers.
The usual alternative to output parameters in C and related languages is to return a single [[data structure]] containing all return values.<ref name=hallam/> For example, given a structure encapsulating width and height, one can write:
<syntaxhighlight lang="c">
WidthHeight width_and_height = F(x);
|