Introduction
In programming, parameters and arguments are commonly used in functions. Although these terms are often used interchangeably, they have subtle differences. Today, I'll explain those differences so you can better understand and identify them.
Parameter
A parameter is a variable defined in a function to receive values, enabling the function to operate on those inputs. Values are passed through these variables, and in a function, these variables are known as parameters. A function can have more than one parameter, and each parameter’s name should be unique. You can check this example for better understanding:
function sum(a, b) {
return a + b;
}
console.log(sum(5, 5)); // output: 10
In this example, the function defines two parameters, a and b, which must each have unique names.
Argument
An argument is a value passed to the variable (parameter). The values that are passed to the parameters are known as arguments in a function. These values are assigned to the parameters, which are then used in the function. Values can be of any type, as there are no restrictions on what can be passed as arguments. You can check this example below:
function sum(a, b) {
return a + b;
}
console.log(sum(5, 5));
// output = 10;
Now, as you can see, values have been passed at the end of the function with sum(5, 5). The first 5 is assigned to parameter a, while the second 5 is assigned to parameter b.
Difference Between Parameter and Argument
Parameter
- A parameter is a variable used in a function for passing values.
- A function can have more than one parameter.
- Each parameter’s name should be unique.
Argument
- An argument is a value that is passed to the variable (parameter).
- A function can have the same number of arguments as parameters, ensuring proper execution.
- An argument can have the same value as another.