Parameters and arguments
Learn the difference between parameters and arguments.
Parameters vs arguments
The names written in the parentheses of a function declaration are called parameters. The values supplied in the parentheses when the function is called are arguments. These two terms are often confused.
Example: in function greet(name) { ... }, name is a parameter. In the call greet("Alex"), "Alex" is an argument. A parameter is a placeholder; an argument is the actual value for that call.
Multiple parameters
A function can take more than one parameter; you separate them with commas. For example, function makeSentence(subject, verb, object) { ... } takes three parameters.
When calling it, you pass arguments in the same order, comma-separated: makeSentence("Cat", "chases", "mouse"). The first argument binds to the first parameter, the second to the second. This is called parameter binding.
Exercise Add two numbers
Define a function called addNumbers. It should take two parameters: a and b. Return the value a + b. Then print the result with console.log(addNumbers(7, 5)).
Show example solution
function addNumbers(a, b) {
return a + b;
}
console.log(addNumbers(7, 5));
Test yourself
-
In the code below, what is '3'?
function mul(a, b) { return a * b; } mul(3, 4);- Parameter
- Return value
- Argument
- Function name
Show answer
Argument — Correct. 3 is the value given at call time, so it is an argument.
-
Provide the right argument
function greet(name) { console.log("Hi " + name); } greet(____);Show answer
function greet(name) { console.log("Hi " + name); } greet("Alex"); -
Match parameter and argument
- function f(x)
- x is a parameter
- f(5)
- 5 is an argument
- function g(a, b)
- a, b are parameters
- g(2, 3)
- 2, 3 are arguments