2.8 Functions Types
The string concatenations from the previous lesson are not the cleanest way to build messages — later in the course we'll see how template literals make this nicer. For now, let's focus on the two main kinds of functions you'll write in JavaScript: functions that perform a task and functions that calculate a value.
Task functions vs value functions
The greet function we wrote before performs a task: it prints a message on the console. It doesn't give anything back to the caller. Other functions compute and return a value using the return keyword. return is a reserved word, so it cannot be used as a variable name.
function square(number) {
return number * number;
}
let result = square(2);
console.log(result); // 4
Because square returns a value, you can store it in a variable or use it directly inside another expression. For example, you don't even need the intermediate variable:
console.log(square(2)); // 4
How many function calls are in that single line? Two. square(2) is one call that returns 4, and console.log(...) is another call that receives that 4 as its argument. The engine evaluates the inner call first, then passes its result to the outer one.
- An argument to a function can be a literal (
'hello',42) or any expression. - An expression can itself be another function call, like
square(2). - A real application is essentially hundreds or thousands of functions working together.
That's the foundation of functions. Later in the course an entire section is dedicated to advanced patterns. In the next section we'll start exploring the different operators in JavaScript.
Summary
This lesson introduces the two fundamental types of functions in JavaScript: functions that perform a task (such as displaying output on the console) and functions that calculate a value and return it using the return keyword. The video demonstrates how function return values can be assigned to variables or passed directly as arguments to other functions, enabling nested function calls. The key insight is that every real-world application is built from hundreds or thousands of functions working together to deliver functionality.
Key points
- Functions can either perform a task (like console.log) or calculate and return a value using the return keyword
- Return values can be stored in variables or passed directly as arguments to other functions without needing an intermediate variable
- Nested function calls (like console.log(square(2))) count as multiple separate function calls—each pair of parentheses represents one function invocation
- The return keyword is a reserved word that sends a computed value back to the caller, enabling functions to communicate results
- Real applications are composed of hundreds or thousands of functions collaborating together to provide all the application's features
FAQ
What is the difference between a function that performs a task and one that returns a value?
A function that performs a task executes instructions and produces a side effect (like displaying output with console.log), while a function that returns a value computes something and sends that result back using the return keyword so the caller can use it.
How many function calls are actually in the code console.log(square(2))?
There are two function calls: first, square(2) is called and returns a value, then console.log() is called with that returned value as its argument.
Why is it beneficial to pass function return values directly as arguments instead of storing them in variables first?
Passing return values directly as arguments reduces unnecessary variable declarations and keeps code cleaner. The JavaScript engine executes the inner function call, gets its return value, and immediately passes it to the outer function.