Parameters (computer Science) - Parameters and Arguments

Parameters and Arguments

These two terms parameter and argument are sometimes loosely 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 value passed. To avoid confusion, it is common to view a parameter as a variable, and an argument as a value.

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 values supplied to the procedure when it is called. 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 more properly thought of as the actual values or references assigned to the parameter variables when the subroutine is called at 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's common to pass in an argument of type void* and cast it to an expected type:

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

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.

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.

Read more about this topic:  Parameters (computer Science)

Famous quotes containing the words parameters and/or arguments:

    Men have defined the parameters of every subject. All feminist arguments, however radical in intent or consequence, are with or against assertions or premises implicit in the male system, which is made credible or authentic by the power of men to name.
    Andrea Dworkin (b. 1946)

    Compared to football, baseball is almost an Oriental game, minimizing individual stardom, requiring a wide range of aggressive and defensive skills, and filled with long periods of inaction and irresolution. It has no time limitations. Football, on the other hand, has immediate goals, resolution on every single play, and a lot of violence—itself a highlight. It has clearly distinguishable hierarchies: heroes and drones.
    Jerry Mander, U.S. advertising executive, author. Four Arguments for the Elimination of Television, ch. 15, Morrow (1978)