Parameter (computer science)
From Wikipedia, the free encyclopedia
A parameter is a variable which can be accepted by a subroutine. The subroutine uses the values assigned to its parameters to alter its behavior at runtime. Most programming languages can define subroutines that accept zero or more parameters.
Contents |
[edit] Parameters and Arguments
Parameters are also commonly referred to as arguments, though arguments are more properly thought of as the actual values or references assigned to the parameter variables when the subroutine is called at runtime. 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.
Many programmers use parameter and argument interchangeably, depending on context to distinguish the meaning. In practice, distinguishing between the two terms is usually unnecessary in order to use them correctly or communicate their use to other programmers. Alternatively, the equivalent terms formal parameter and actual parameter may be used.
To better understand the difference, consider the following subroutine written in C:
int sum(int addend1, int addend2) { return addend1 + addend2; }
The subroutine 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 subroutine might look like this:
int sumValue; int value1 = 40; int value2 = 2; sumValue = sum(value1, value2);
The variables value1 and value2 are initialized with values. The variables are not arguments or parameters.
At runtime, the values assigned to these variables are passed to the subroutine sum. In the sum subroutine, 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 sumValue.
[edit] Datatypes
In strongly-typed programming languages that are explicitly typed, each parameter's type is specified in the subroutine's declaration. Languages using type inference attempt to discover the types automatically from the function's body and usage, while weakly-typed programming languages defer type resolution to run-time.
Some languages use a special keyword (e.g. void) to indicate that the subroutine has no parameters; in formal type theory, such functions take an empty parameter list (whose type is not void, but rather unit).
[edit] 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.
[edit] Default Arguments
Some programming languages allow for a default argument to be explicitly or implicity given in a subroutine's declaration. This allows the caller to omit that argument when calling the subroutine. 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.
[edit] Variable-length Parameter Lists
Some languages allow subroutines to be defined to accept a variable number of arguments. For such languages, the subroutines must iterate through the list of arguments.
[edit] Named Parameters
Some programming languages allow subroutines 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.