Calling Functions and Return Values
What you'll learn: How to invoke (call) a function and capture the value it sends back to you.
What Does "Calling a Function" Mean?
When you declare a function, you're just defining instructions—nothing runs yet. To actually execute those instructions, you must call (or invoke) the function by writing its name followed by parentheses: functionName().
Think of a function like a vending machine. Declaring it is like installing the machine. Calling it is like pressing a button—only then do you get something out.
The Return Value
Functions can send a value back to the place where they were called using the return keyword. This value is called the return value. If a function doesn't explicitly return anything, it returns undefined by default.
function getGreeting() {
return "Hello!";
}
// Call the function and capture its return value
let message = getGreeting();
// message now holds "Hello!"
Using the Return Value
You can use the returned value immediately in expressions, assignments, or even pass it to other operations:
function double(num) {
return num * 2;
}
let result = double(5); // result is 10
let total = double(3) + double(4); // total is 6 + 8 = 14
Return Ends Execution
When JavaScript hits a return statement, the function stops immediately—no code after return runs.
Key Takeaway: Calling a function executes its code, and return sends a value back to the caller; without return, the function returns undefined.