Content deleted Content added
Stevebroshar (talk | contribs) →Example: it's not a program; edit for flow |
Stevebroshar (talk | contribs) →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.
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 45 ⟶ 37:
</syntaxhighlight>
The
<syntaxhighlight lang="c">
int value1 = 40;
Line 53 ⟶ 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 100 ⟶ 88:
== Argument passing ==
The
=== Default arguments ===
|