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!
== 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 [[Declaration (computer programming)|declaration]], while ''argument'' (sometimes called ''actual parameter'') refers to the actual input supplied at a function call statement. For example, if one defines a function as <code>def f(x): ...</code>, then <code>x</code> is the parameter, and if it is called by <code>a = ...; f(a)</code> then <code>a</code> is the argument. A parameter is an (unbound) variable, while the argument can be a [[Literal (computer programming)|literal]] or variable or more complex expression involving literals and variables. In case of call by value, what is passed to the function is the value of the argument β for example, <code>f(2)</code> and <code>a = 2; f(a)</code> are equivalent calls β while in call by reference, with a variable as argument, what is passed is a reference to that variable - even though the syntax for the function call could stay the same.<ref>{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference|title=Passing Arguments by Value and by Reference (Visual Basic)|last=Dollard|first=Kathleen|website=Microsoft Learn |language=en-us|access-date=2018-10-27}}</ref> The specification for [[Call_by_reference|pass-by-reference]] or [[Call by value|pass-by-value]] would be made in the function declaration and/or definition. 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. A parameter is an intrinsic property of the procedure, included in its definition. For example, in many languages, a procedure to add two supplied integers together and calculate the sum would need two parameters, one for each integer. In general, a procedure may be defined with any number of parameters, or no parameters at all. If a procedure has parameters, the part of its definition that specifies the parameters is called its ''parameter list''. 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 subroutine is called at [[Run time (program lifecycle phase)|run-time]]. When discussing code that is calling into a subroutine, any values or references passed into the subroutine 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 subroutine definition, the variables in the subroutine'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"> 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 // subroutine 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) { return addend1 + addend2; } </syntaxhighlight> The function ''Sum'' has two parameters, named ''addend1'' and ''addend2''. It adds the values passed into the parameters, and returns the result to the subroutine's caller (using a technique automatically supplied by the C compiler). The code which calls the ''Sum'' function might look like this: <syntaxhighlight lang="c"> int value1 = 40; int value2 = 2; 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]]. ===Alternative convention in Eiffel=== Within the [[Eiffel (programming language)|Eiffel]] software development method and language, the terms ''argument'' and ''parameter'' have distinct uses established by convention. The term ''argument'' is used exclusively in reference to a routine's inputs,<ref>Meyer, Bertrand. ''[[Object-Oriented Software Construction]], 2nd Edition,'' Prentice Hall, 1997, p 444.</ref> and the term ''parameter'' is used exclusively in type parameterization for [[Generic programming|generic classes]].<ref>Meyer, p. 96.</ref> Consider the following routine definition: <syntaxhighlight lang="eiffel"> sum (addend1: INTEGER; addend2: INTEGER): INTEGER do Result := addend1 + addend2 end </syntaxhighlight> The routine <code>sum</code> takes two arguments <code>addend1</code> and <code>addend2</code>, which are called the routine's '''formal arguments'''. A call to <code>sum</code> specifies '''actual arguments''', as shown below with <code>value1</code> and <code>value2</code>. <syntaxhighlight lang="eiffel"> sum_value: INTEGER value1: INTEGER = 40 value2: INTEGER = 2 β¦ sum_value := sum (value1, value2) </syntaxhighlight> Parameters are also thought of as either '''formal''' or '''actual'''. '''Formal generic parameters''' are used in the definition of generic classes. In the example below, the class <code>HASH_TABLE</code> is declared as a generic class which has two formal generic parameters, <code>G</code> representing data of interest and <code>K</code> representing the hash key for the data: <syntaxhighlight lang="eiffel"> class HASH_TABLE [G, K -> HASHABLE] β¦ </syntaxhighlight> When a class becomes a client to <code>HASH_TABLE</code>, the formal generic parameters are substituted with '''actual generic parameters''' in a '''generic derivation'''. In the following attribute declaration, <code>my_dictionary</code> is to be used as a character string based [[Associative array|dictionary]]. As such, both data and key formal generic parameters are substituted with actual generic parameters of type <code>STRING</code>. <syntaxhighlight lang="eiffel"> my_dictionary: HASH_TABLE [STRING, STRING] </syntaxhighlight>
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)