In computer programming, a parameter, or formal argument, is a variable that represents an argument to a function call. A function's signature defines its parameters. A call invocation involves evaluating each argument expression of a call and associating the result with the corresponding parameter.
For example, consider the Python function def add(x: int, y: int) -> int: return x + y Variables x and y are parameters, each of type int. For call add(2, 3), the expressions 2 and 3 are arguments. For call add(a + 1, b + 2), the arguments are a + 1 and b + 2.
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 function. Generally, with call by value, a parameter acts like a new, local variable initialized to the value of the argument. If the argument is a variable, the function cannot modify the argument state because the parameter is a copy. With call by reference, which requires the argument to be a variable, the parameter is an alias of the argument.
Example The following C source code defines a function named with one parameter named ; both the function and parameter are typed . For call , the argument is passed to the function as the double value 10 and assigned to parameter variable , and the function returns 0.5.
double salesTax(double price) { return 0.05 * price; }
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, 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 def f(x): ..., then x is the parameter, and if it is called by a = ...; f(a) then a is the argument. A parameter is an (unbound) variable, while the argument can be a 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, f(2) and a = 2; f(a) 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. The specification for pass-by-reference or 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 f(x) = x * x the variable x is a parameter; in the function call f(2) 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 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. 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 function, sum, which has two parameters, a and b. It adds the values passed into the parameters, and returns the result to the function's caller.
int sum(int a, int b) { return a + b; }
The following is an example of calling sum. The variables x and y are initialized and then passed to sum as the arguments. At runtime, the values assigned to these variables are passed to sum. In sum, the parameters a and b 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 result.
int x = 40; int y = 2; int result = sum(x, y);
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 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,Meyer, Bertrand. Object-Oriented Software Construction, 2nd Edition, Prentice Hall, 1997, p 444. and the term parameter is used exclusively in type parameterization for generic classes.Meyer, p. 96.
Consider the following routine definition:
sum (a: INTEGER; b: INTEGER): INTEGER do Result := a + b end
The routine sum takes two arguments a and b, which are called the routine's formal arguments. A call to sum specifies actual arguments, as shown below with x and y.
result: INTEGER x: INTEGER = 40 y: INTEGER = 2 … result := sum (x, y)
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 HASH_TABLE is declared as a generic class which has two formal generic parameters, G representing data of interest and K representing the hash key for the data:
class HASH_TABLE [G, K -> HASHABLE] …
When a class becomes a client to HASH_TABLE, the formal generic parameters are substituted with actual generic parameters in a generic derivation. In the following attribute declaration, my_dictionary is to be used as a character string based dictionary. As such, both data and key formal generic parameters are substituted with actual generic parameters of type STRING.
my_dictionary: HASH_TABLE [STRING, STRING]
Datatypes In strongly typed programming languages, each parameter's 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 function has no parameters; in formal type theory, such functions take an empty parameter list (whose type is not void, but rather unit).
Argument passing The 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 Some programming languages such as Ada, C++, Clojure, Common Lisp, Fortran 90, Python, Ruby, Tcl, and Windows PowerShell allow for a default argument to be explicitly or implicitly given in a function's declaration. This allows the caller to omit that argument when calling the function. If the default argument is explicitly given, then that value is used if it is not provided by the caller. If the default argument is implicit (sometimes by using a keyword such as Optional) then the language provides a well-known value (such as null, Empty, zero, an empty string, etc.) if a value is not provided by the caller.
PowerShell example: function doc($g = 1.21) { "$g gigawatts? $g gigawatts? Great Scott!" } PS > doc 1.21 gigawatts? 1.21 gigawatts? Great Scott!
PS > doc 88 88 gigawatts? 88 gigawatts? Great Scott! Default arguments can be seen as a special case of the variable-length argument list.
Variable-length parameter lists Some languages allow functions to be defined to accept a variable number of arguments. For such languages, the functions must iterate through the list of arguments.
PowerShell example: function marty { $args | foreach { "back to the year $_" } } PS > marty 1985 back to the year 1985
PS > marty 2015 1985 1955 back to the year 2015 back to the year 1985 back to the year 1955
Named parameters Some programming languages—such as Ada and Windows PowerShell—allow functions to have named parameters. This allows the calling code to be more self-documenting. It also provides more flexibility to the caller, often allowing the order of the arguments to be changed, or for arguments to be omitted as needed.
PowerShell example: function jennifer($adjectiveYoung, $adjectiveOld) { "Young Jennifer: I'm $adjectiveYoung!" "Old Jennifer: I'm $adjectiveOld!" } PS > jennifer 'fresh' 'experienced' Young Jennifer: I'm fresh! Old Jennifer: I'm experienced!
PS > jennifer -adjectiveOld 'experienced' -adjectiveYoung 'fresh' Young Jennifer: I'm fresh! Old Jennifer: I'm experienced!
Multiple parameters in functional languages In lambda calculus, each function has exactly one parameter. What is thought of as functions with multiple parameters is usually represented in lambda calculus as a function which takes the first argument, and returns a function which takes the rest of the arguments. This is a transformation named currying. Some programming languages, like ML and Haskell, follow this scheme. In these languages, every function has exactly one parameter, and what may look like the definition of a function of multiple parameters, is actually syntactic sugar for the definition of a function that returns a function, etc. Function application is left-associative in these languages and in lambda calculus, so what looks like an application of a function to multiple arguments is correctly evaluated as the function applied to the first argument, then the resulting function applied to the second argument, etc.
Output parameters An output parameter, also known as an out parameter or return parameter, is a parameter used for output, rather than the more usual use for input. Using call by reference parameters, or call by value parameters where the value is a reference, as output parameters is an idiom in some languages, notably C and C++, while other languages have built-in support for output parameters. Languages with built-in support for output parameters include Ada8.2 Parameter Modes, "Rationale for the Design of the Ada® Programming Language" (see Ada subprograms), Fortran (since Fortran 90; see Fortran "intent"), various procedural extensions to SQL, such as PL/SQL (see PL/SQL functions)8. PL/SQL Subprograms: Specifying Subprogram Parameter Modes and Transact-SQL, C# and the .NET Framework,ParameterDirection Enumeration Swift,Functions — The Swift Programming Language (Swift 4.2) and the scripting language TScript (see TScript function declarations).
Here is an example of an "output parameter" in C++:
// Definition of a function with an output parameter