Parameter (computer programming): Difference between revisions

Content deleted Content added
Example: it's not a program; edit for flow
Argument passing: exact adds no value
 
(2 intermediate revisions by the same user not shown)
Line 8:
 
==Example==
The following [[C (programming language)|C]] [[source code]] defines a function named {{code |SalesTax}} with one parameter named {{code |price}}; both typed {{code |double}}. For call {{code |SalesTax(10.00)}}, the argument {{code|10.00}} is passed to the function as the [[Double-precision floating-point format |double]] value 10 and assigned to parameter variable {{code |price}}, and the function returns 0.5.
 
<syntaxhighlight lang="c">
Line 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 function is called at [[Run time (program lifecycle phase)|run-time]]. When discussing code that is calling into a function, any values or references passed into the function are the arguments, and the place in the code where these values or references are given is the ''parameter list''. When discussing the code inside the function definition, the variables in the function's parameter list are the parameters, while the values of the parameters at runtime are the arguments. For example, in C, when dealing with threads it is common to pass in an argument of type void* and cast it to an expected type:
 
<syntaxhighlight lang="c">
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.
void ThreadFunction(void* pThreadArgument)
{
// Naming the first parameter 'pThreadArgument' is correct, rather than
// 'pThreadParameter'. At run time the value we use is an argument. As
// mentioned above, reserve the term parameter for when discussing
// function definitions.
}
</syntaxhighlight>
 
To better understand the difference, consider the following function written in [[C (programming language)|C]]:
<syntaxhighlight lang="c">
int Sum(int addend1, int addend2)
Line 45 ⟶ 37:
</syntaxhighlight>
 
The functionfollowing is an example of calling ''Sum''. hasThe twovariables parameters,''value1'' namedand ''addend1value2'' are initialized and then passed to ''addend2Sum'' as the arguments. ItAt addsruntime, the values assigned to these variables are passed intoto ''Sum''. In ''Sum'', the parameters ''addend1'' and ''addend2'' are evaluated, yielding the arguments 40 and returns2, respectively. The values of the arguments are added, and the result is returned to the function's caller, (usingwhere ait techniqueis automaticallyassigned supplied byto the Cvariable compiler)''sum_value''.
 
The code which calls the ''Sum'' function might look like this:
<syntaxhighlight lang="c">
int value1 = 40;
Line 53 ⟶ 44:
int sum_value = Sum(value1, value2);
</syntaxhighlight>
The variables ''value1'' and ''value2'' are initialized with values. ''value1'' and ''value2'' are both arguments to the ''sum'' function in this context.
 
At runtime, the values assigned to these variables are passed to the function ''Sum'' as arguments. In the ''Sum'' function, the parameters ''addend1'' and ''addend2'' are evaluated, yielding the arguments 40 and 2, respectively. The values of the arguments are added, and the result is returned to the caller, where it is assigned to the variable ''sum_value''.
 
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 100 ⟶ 88:
 
== 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 ===