• Programming
• Internet

# Bits & Bytes

## Posts Tagged ‘value’

### Arguments and Parameters

The terms argument and parameter are frequently used interchangeably, and there is often confusion about what these two terms mean. Arguments and parameters are two different things, but they are closely related. By definition, an argument is a value or variable that is passed into a function, and a parameter is value or variable that is used inside of a function. For illustration, look at the program below.

```#include <iostream>

int Sum(int iP1, int iP2) {
return iP1 + iP2;
}

int main () {
int iA1 = 48;
int iA2 = 24;

std::cout << Sum(iA1, iA2) << std::endl;

return 0;
}```

The program contains takes in two int values and returns an int that is the sum of them. The two int values that the function takes in are called arguments, while the two values that are used inside the function are called parameters. To clarify this, we have named the variables that we used for the arguments, iA1 and iA2, and the two variables that we used for the parameters, iP1 and iP2. Correspondingly, "iA1, iA2" inside the function call is called the argument list and "int iP1, int iP2" inside the function definition is called the parameter list.

In the example above, the difference between the arguments and paramters is clear. The arguments and parameters refer to totally different memory locations because the arguments are both passed by value. In the program below, we have replaced the variables with the constant literals 48 and 24. In this program, these literals are the arguments.

```#include <iostream>

int Sum(int iP1, int iP2) {
return iP1 + iP2;
}

int main () {

std::cout << Sum(48, 24) << std::endl;

return 0;
}```

In our last example below, we changed the function a bit; we pass the first argument by reference and use a default argument for the second. So, the arguments are iA and 45. The parameters are still iP1 and iP2. However, the first parameter is only a reference so changing its value changes the value of iA, as we would expect. Passing values by reference is probably one of the sources of confusion between arguments and parameters, but it is easy to understand if we remember that the parameter is only a reference; in fact, we could use any valid C++ data type as a parameter, including pointers, constants, etc.

```#include <iostream>

void AddTo(int& iP1, int iP2 = 45) {
iP1 += iP2;
}

int main () {
int iA = 16;