Chapter VI · Functions
What is a Function?
A function is a reusable block of code designed to perform a particular task when invoked.
01 How It Works
Think of a vending machine: you insert inputs, internal machinery runs, and an item is returned.
Functions encapsulate logic so you can write code once and execute it multiple times with different inputs.
example.js
function greet() {
console.log("Hello, World!");
}
greet();
1. Define the routine using the function keyword.
2. Group statements inside curly braces.
3. Execute the function using parentheses.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
function printWelcome() {
console.log("Welcome to the application!");
}
printWelcome();
Key Takeaway
Functions promote code reusability and modular design.