Parameters & Return Values
What you'll learn:
- How parameters let you pass different inputs to the same function
- How
returnsends a value back out of a function - How to store a returned value in a variable
- The difference between a function that prints vs one that returns
Parameters — giving a function inputs
A parameter is a variable listed inside the function's parentheses. When you call the function, the value you pass in becomes that variable inside the function.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // prints: Hello, Alice!
greet("Bob"); // prints: Hello, Bob!
The same function, two different results, because the input changed.
Return — getting a value back out
console.log inside a function prints something, but the result disappears. If you want to use the result later, use return:
function add(a, b) {
return a + b;
}
let result = add(3, 4);
console.log(result); // 7
return hands the value back to whoever called the function. You can then store it, print it, or use it in another expression.
Try it: Run the block. Then change the two numbers passed to add and run it again.