Master how functions receive data through parameters, how arguments supply the
actual values, and how return sends a result back — the complete
input-process-output cycle.
Parameters are variables listed in a function definition that act as placeholders for values. Arguments are the actual values passed to the function when it is called — they fill those placeholders.
A return value is the output that a function sends back to
its caller after execution, using the return statement. Without
return, the function automatically returns undefined.
Every function follows the same flow: Input → Process → Output
function add(a, b)
add(5, 3)
function greet(name) { // name = parameter
console.log("Hello " + name);
}
greet("Aman"); // "Aman" = argument
greet("Rahul"); // "Rahul" = argument — same function, different input
name is the parameter — a local variable inside the function"Aman" and "Rahul" are arguments — the values supplied at call timefunction add(a, b) {
return a + b; // sends result back to the caller
}
let result = add(5, 3); // capture the returned value
console.log(result);
// You can also use the return value directly
console.log(add(10, 20));
return a + b exits the function and sends the sum backfunction test() {
console.log("Hi"); // prints, but returns nothing
}
let output = test(); // captures the return value
console.log(output); // what is output?
return statement implicitly returns undefinedconsole.log() prints a value — it does not return onereturn if you need to use the result elsewhere in your codefunction greet(name = "Guest") { // default value if none given
console.log("Hello " + name);
}
greet(); // no argument → uses default "Guest"
greet("Aman"); // argument provided → uses "Aman"
undefined bugs when a caller forgets to pass a valueThink of a food-order system at a restaurant:
Click ▶ Preview, enter two numbers, and watch the function
receive them as arguments, multiply them, and return the result
to the page.
<!DOCTYPE html>
<html>
<head><title>Parameters & Return</title></head>
<body style="font-family:sans-serif;padding:24px;background:#f8f9fa">
<h2>Multiply Numbers</h2>
<input id="n1" type="number" placeholder="Number 1"
style="padding:8px 12px;border:1px solid #ccc;border-radius:6px;
font-size:1rem;width:130px">
<input id="n2" type="number" placeholder="Number 2"
style="padding:8px 12px;border:1px solid #ccc;border-radius:6px;
font-size:1rem;width:130px;margin-left:8px">
<button onclick="calculate()"
style="margin-left:8px;padding:8px 18px;background:#f7df1e;border:none;
border-radius:6px;cursor:pointer;font-weight:700;font-size:1rem">
Multiply
</button>
<div id="output" style="margin-top:16px;line-height:2;font-size:0.95rem"></div>
<script>
// Parameter: a, b — placeholders defined here
function multiply(a, b) {
return a * b; // return sends result back
}
function calculate() {
let a = Number(document.getElementById("n1").value);
let b = Number(document.getElementById("n2").value);
if (!document.getElementById("n1").value ||
!document.getElementById("n2").value) {
document.getElementById("output").innerHTML =
"<b style='color:red'>Please enter both numbers.</b>";
return;
}
// Argument: a, b — actual values passed here
let result = multiply(a, b);
document.getElementById("output").innerHTML =
"<b>multiply(" + a + ", " + b + ")</b> returned: " +
"<b style='color:#e07b00'>" + result + "</b>";
}
</script>
</body>
</html>
function sayHello(name) {
// your code here
}
sayHello("Priya");
name parameter"Friend" for when no argument is passedfunction average(a, b, c) {
// return the average
}
console.log(average(10, 20, 30));
a, b, and c3 and return the resultname in function greet(name). The argument is the actual value you pass when calling it.
return statement?
function noReturn() {
let x = 5;
}
console.log(noReturn());
undefined from any function that does not have an explicit return statement.
function test(a) {
return a * 2;
}
console.log(test(4));
4 is passed to parameter a. The function returns 4 * 2 = 8, which console.log then prints.
Always return a value if you need to use it later. A function that only console.logs cannot be reused in expressions:
// ✗ Not reusable — result is lost
function sumBad(a, b) {
console.log(a + b);
}
// ✓ Reusable — caller decides what to do with the value
function sumGood(a, b) {
return a + b;
}
let total = sumGood(5, 10) * 2; // usable in expressions
console.log(total);
return immediately stops the function — any code after it in the same block is unreachable:
function test() {
return 5;
console.log("Hello"); // never runs — dead code
}
console.log(test()); // 5
Use default parameters to make functions safer — they prevent undefined when a caller forgets an argument:
function power(base, exponent = 2) {
return base ** exponent;
}
console.log(power(3)); // 3² = 9 (uses default)
console.log(power(3, 3)); // 3³ = 27 (overrides default)
Return values can be passed directly as arguments to another function — this is called function chaining:
const double = x => x * 2;
const addTen = x => x + 10;
// Pass the return value of double() directly to addTen()
let result = addTen(double(5));
console.log(result); // double(5)=10, addTen(10)=20
The return value of the first call becomes the argument of the second — trace through each step before running.
function calc(a, b) {
return a + b;
}
let x = calc(2, 3);
let y = calc(x, 4);
console.log(y);
// Step 1: calc(2, 3)
// a=2, b=3 → return 2 + 3 = 5
// x = 5
// Step 2: calc(x, 4) → calc(5, 4)
// a=5, b=4 → return 5 + 4 = 9
// y = 9
// Step 3: console.log(y)
// Output → 9
// Key takeaway:
// The return value of a function becomes a usable value —
// here it is stored in x and then passed as an argument
// to the next call. This is the power of return values:
// functions can be chained and composed together.