In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters.

Just as in standard mathematical usage, the argument is thus the actual input passed to a function, procedure, or routine, whereas the parameter is the variable inside the implementation of the subroutine. For example, if one defines the add subroutine as def add(x, y): return x + y, then x, y are parameters, while if this is called as add(2, 3), then 2, 3 are the arguments. Note that variables from the calling context can be arguments: if the subroutine is called as a = 2; b = 3; add(a, b) then the variables a, b are the arguments, not only the values 2, 3.

In the most common case, call by value, a parameter acts within the subroutine as a variable initialized to the value of the argument (a local(isolated) copy of the argument if the argument is a variable), but in other cases, e.g. call by reference, the argument supplied by the caller can be affected by actions within the called subroutine (as discussed in evaluation strategy). In call by value, one can thus think of arguments as values (properly, think of the value of arguments as the "arguments" themselves), but in general arguments are not simply values.

Example

The following program in the C programming language defines a function that is named "sales_tax" and has one parameter named "price". The type of price is "double" (i.e. a double-precision floating point number). The function's return type is also a double.

double sales_tax(double price)
{
    return 0.05 * price;
}

After the function has been defined, it can be invoked as follows:

sales_tax(10.00);

In this example, the function has been invoked with the number 10.00. When this happens, 10.00 will be assigned to price, and the function begins calculating its result. The steps for producing the result are specified below enclosed in {} "0.05 * price" indicates that the first thing to do is multiply 0.05 by the value of price, which gives 0.50. "return" means the function will produce the result of "0.05 * price". Therefore, the final result (ignoring possible round-off errors one encounters with representing decimal fractions in IEEE-754 format) is 0.50.

Parameter and Argument

The terms parameter and argument are sometimes 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 definition, while argument(sometimes called actual parameter) refers to the actual input passed. For example, if one defines a function as def f(x): ..., then x is the parameter, while if it called by a = ...; f(a) then a is the argument. In both cases a parameter is an (unbound) variable, while the argument can be thought of as a value or variable, depending on the calling convention. In case of call by value, one can think of the argument as a value (properly, as the value of the argument) – for example, f(2) and a = 2; f(a) are equivalent calls – while in call by reference one can think of the argument as a variable in the calling context.

To better understand the difference, consider the following function written in C:

int sum(int addend1, int addend2)
{
    return addend1 + addend2;
}

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:

int sumValue;
int value1 = 40;
int value2 = 2;

sumValue = sum(value1, value2);

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 sumValue.

You have no rights to post comments