Chapter VI · Functions
Return Values
The return statement specifies the output value that a function outputs back to its caller.
01 How It Works
Think of a cash machine dispensing requested money bills before closing the slot.
When execution hits a return statement, the function stops immediately and yields the specified value.
example.js
function add(a, b) {
return a + b;
}
let result = add(2, 3); // 5
1. Compute data inside the function body.
2. Use the return keyword followed by the output expression.
3. Capture the result in a variable upon invocation.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
function getCube(x) {
return x * x * x;
}
let val = getCube(3); // 27
Key Takeaway
Functions output values back to callers via the return keyword.