Content deleted Content added
No edit summary Tags: Reverted Mobile edit Mobile web edit |
Stevebroshar (talk | contribs) →Argument passing: exact adds no value |
||
(35 intermediate revisions by 17 users not shown) | |||
Line 1:
{{Short description |
{{Other uses |Parameter (disambiguation)}}
In [[computer programming]], a '''parameter'''
==Example==
The following
<syntaxhighlight lang="c">
double SalesTax(double price)
Line 15 ⟶ 16:
}
</syntaxhighlight>
== Parameters and arguments ==
The terms ''parameter'' and ''argument'' may have different meanings in different programming languages. Sometimes they are used interchangeably, and the context is used to distinguish the meaning. The term ''parameter'' (sometimes called ''formal parameter'') is often used to refer to the variable as found in the function
Parameters appear in procedure definitions; arguments appear in procedure calls. In the function definition <code>f(x) = x*x</code> the variable <var>x</var> is a parameter; in the function call <code>f(2)</code> the value 2 is the argument of the function. Loosely, a parameter is a type, and an argument is an instance.
Line 32 ⟶ 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 51 ⟶ 37:
</syntaxhighlight>
The
<syntaxhighlight lang="c">
int value1 = 40;
Line 59 ⟶ 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 103 ⟶ 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 127 ⟶ 109:
=== Variable-length parameter lists ===
Some languages allow
PowerShell example:
Line 146 ⟶ 128:
=== Named parameters ===
Some programming languages—such as [[Ada (programming language)|Ada]] and [[Windows PowerShell]]—allow
PowerShell example:
Line 209 ⟶ 191:
where <code>x</code> is an input parameter and <code>width</code> and <code>height</code> are output parameters.
A common use case in C and related languages is for [[exception handling]], where a function places the return value in an output variable, and returns a
<syntaxhighlight lang="csharp">
public static bool TryParse(string s, out int result)
Line 267 ⟶ 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);
|