Parameter
- In Turkish
- Parametre
- Pronunciation
- puh-RAM-uh-ter
In short
A parameter is a named input in a function's definition, and an argument is the actual value passed in that place when the function is called.
What is a parameter in programming?
In function area(width, height), width and height are parameters: placeholders the function uses in its body. When the code calls area(3, 4), the values 3 and 4 are arguments, and inside that call width is 3 and height is 4. The terms are often used interchangeably in conversation, but the distinction helps when reading documentation and error messages.
Languages offer several ways to pass arguments. Positional arguments are matched by order; named or keyword arguments are matched by name, as in Python's area(width=3, height=4); default parameters supply a value when the caller leaves one out; and rest or variadic parameters, such as ...items in JavaScript or *args in Python, collect any number of extra arguments.
How an argument reaches the function also matters. Primitive values such as numbers are copied, so changing the parameter doesn't affect the caller. Objects and lists are usually passed as references, so a function that modifies the object it received changes the caller's object too, even though reassigning the parameter itself does not.
A common misconception is that a function with many parameters is flexible. Long parameter lists are hard to call correctly, because it is easy to swap two values of the same type. Grouping related values into one options object, or using named arguments, makes calls clearer and safer.
Key takeaways
- Parameters are the named inputs in a function definition.
- Arguments are the values passed in when the function is called.
- Arguments can be positional, named, defaulted or variadic.
- Objects are usually passed by reference, so changes can reach the caller.
- Long parameter lists are error-prone; an options object helps.
Example
// width and height are parameters; unit has a default value
function area(width, height, unit = "m²") {
return `${width * height} ${unit}`;
}
area(3, 4); // 3 and 4 are arguments → "12 m²"
area(3, 4, "cm²"); // overrides the default
// A rest parameter collects any number of arguments
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
sum(1, 2, 3, 4); // 10
// An options object instead of a long parameter list
function createUser({ name, email, admin = false }) { /* ... */ }
createUser({ name: "Ada", email: "ada@example.com" });Readers ask
What is the difference between a parameter and an argument?
A parameter is the variable named in the function definition. An argument is the value supplied for it in a particular call. In greet(name) called as greet("Ada"), name is the parameter and "Ada" is the argument.
What is a default parameter?
A parameter with a value that is used when the caller doesn't pass one, such as function greet(name = "friend"). It keeps calls short for the common case.
Are arguments passed by value or by reference?
It depends on the language and the type. Most languages copy primitive values and pass objects as a reference to the same object, so mutating the object inside the function is visible outside, while reassigning the parameter is not.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- MethodProgramming Fundamentals, p. 37A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").
- Lambda FunctionProgramming Fundamentals, p. 33A lambda function is a small function written inline without a name, usually passed straight to another function such as a sorting or filtering helper.
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
Spotted a mistake or something missing on this page?Suggest an edit