Calculating totals for several products repeats the same operation with different values. A function gives that operation a name so we can call it instead of copying its implementation.
A definition is not a call
function calculateTotal(unitPrice, quantity) {
return unitPrice * quantity;
}
const firstTotal = calculateTotal(120, 2);
const secondTotal = calculateTotal(80, 3);
console.log(firstTotal);
console.log(secondTotal);
The function declaration defines the calculation. unitPrice and quantity are parameters: names for the values each call will supply. Defining the function does not immediately print a multiplication result.
The call calculateTotal(120, 2) supplies 120 and 2 as arguments. It returns 240, which we assign to firstTotal. The second call uses different inputs and also returns 240. Different inputs can legitimately produce the same result.
Where does return go?
return supplies a value to the calling expression and ends that function call. We store the result in a variable, but could also use console.log(calculateTotal(50, 4)) directly.
The parameter names belong to the call's local work. In this example we want to produce a result without modifying an unrelated outer price variable.
Logging is not returning
function showTotal(price, quantity) {
console.log(price * quantity);
}
const result = showTotal(120, 2);
console.log(result);
You see 240 and then undefined. The first message was logged inside the function. Because the function supplied no explicit return value, its call evaluated to undefined. Printing a value is not enough if another calculation needs to use it.
A function can also intentionally change the page without returning a useful value. That is a different purpose, not automatically a mistake. Ask whether the function should produce a value or perform an effect.
Walk through one call
At calculateTotal(120, 2), argument expressions are evaluated and their values initialize the call's parameters. The body multiplies them, returns 240, and the calling expression continues with that value. The surrounding assignment therefore receives a number.
Renaming firstTotal outside the function does not rename an internal parameter. A parameter is a name in the definition; an argument is the value or expression supplied by a call.
What runs after return?
function double(value) {
return value * 2;
console.log('This line is not reached.');
}
console.log(double(4));
Only 8 is printed. Return ends that invocation, not the entire script or browser. Code after the call can continue.
An early return can handle unsuitable input, but the result's meaning must be clear to the caller. Inconsistently returning numbers and unrelated error text makes later code harder to use.
A newline after return matters
function brokenDouble(value) {
return
value * 2;
}
console.log(brokenDouble(4));
This returns undefined. JavaScript treats the immediate line break specially; the indented expression is not the returned expression. Keep it on the same line as return in these introductory examples.
Default a missing argument deliberately
function addDelivery(total, delivery = 0) {
return total + delivery;
}
console.log(addDelivery(240));
console.log(addDelivery(240, 35));
The outputs are 240 and 275. Omitting the second argument uses zero. A default is not general validation of null or arbitrary text. It supplies a meaningful starting value for an absent argument.
Combine the multiplication function with addDelivery, first through intermediate variables, then by nesting calls. Trace which value returns from each invocation. Understanding that flow is more important than copying a function declaration.
A missing argument is still missing
calculateTotal(120) supplies no second argument. The quantity parameter is undefined, and the multiplication produces NaN. Declaring a function does not automatically validate its inputs.
Call calculateTotal(75, 4) and check for 300. Then add delivery to the returned value. You have reused the operation's result without copying the calculation itself.
References: MDN