|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: "JavaScript Functions" |
| 3 | +sidebar_label: "3. Functions" |
| 4 | +sidebar_position: 3 |
| 5 | +description: "Learn how to write reusable blocks of code to make your programs efficient." |
| 6 | +--- |
| 7 | + |
| 8 | +Imagine you are a chef. Instead of writing down the steps to bake a cake every single time someone orders one, you just write the recipe once and give it a name: **"BakeCake"**. Whenever you need a cake, you just follow that named recipe. |
| 9 | + |
| 10 | +In JavaScript, a **Function** is a reusable block of code designed to perform a particular task. |
| 11 | + |
| 12 | +## 1. How to Define a Function |
| 13 | + |
| 14 | +Creating a function is like building a machine. It has three main parts: |
| 15 | + |
| 16 | +1. **Declaration:** Using the `function` keyword. |
| 17 | +2. **Name:** A descriptive name (using camelCase). |
| 18 | +3. **Parameters:** Values you "feed" into the machine (inside the parentheses `()`). |
| 19 | +4. **Body:** The actual code inside the curly braces `{}`. |
| 20 | + |
| 21 | +```javascript |
| 22 | +function sayHello(name) { |
| 23 | + console.log("Hello " + name + "! Welcome to CodeHarborHub."); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## 2. Calling (Using) a Function |
| 28 | + |
| 29 | +Defining a function doesn't run the code inside. To use the machine, you must **"Call"** it by using its name followed by parentheses. |
| 30 | + |
| 31 | +```javascript |
| 32 | +// Calling the function |
| 33 | +sayHello("Ajay"); |
| 34 | +// Output: Hello Ajay! Welcome to CodeHarborHub. |
| 35 | + |
| 36 | +sayHello("Student"); |
| 37 | +// Output: Hello Student! Welcome to CodeHarborHub. |
| 38 | +``` |
| 39 | + |
| 40 | +## 3. Return Values |
| 41 | + |
| 42 | +Sometimes, you don't just want a function to print a message; you want it to **give you something back** (like a calculator giving you the result of a sum). We use the `return` keyword for this. |
| 43 | + |
| 44 | +```javascript |
| 45 | +function addNumbers(a, b) { |
| 46 | + return a + b; // Sends the result back to whoever called it |
| 47 | +} |
| 48 | + |
| 49 | +let result = addNumbers(5, 10); |
| 50 | +console.log(result); // Output: 15 |
| 51 | +``` |
| 52 | + |
| 53 | +:::warning The Return Rule |
| 54 | +Once a function hits a `return` statement, it stops immediately. Any code written *after* the return inside that function will be ignored. |
| 55 | +::: |
| 56 | + |
| 57 | +## 4. Parameters vs. Arguments |
| 58 | + |
| 59 | +These terms are often confused, but the difference is simple: |
| 60 | + |
| 61 | +* **Parameters:** The "placeholders" listed in the function definition (e.g., `a` and `b`). |
| 62 | +* **Arguments:** The "real values" you pass to the function when you call it (e.g., `5` and `10`). |
| 63 | + |
| 64 | +## 5. Arrow Functions (The Modern Way) |
| 65 | + |
| 66 | +In modern JavaScript (ES6+), developers use a shorter syntax called **Arrow Functions**. It's the "cool" way to write functions at **CodeHarborHub**. |
| 67 | + |
| 68 | +<Tabs> |
| 69 | + <TabItem value="traditional" label="Traditional" default> |
| 70 | + ```js |
| 71 | + function multiply(x, y) { |
| 72 | + return x * y; |
| 73 | + } |
| 74 | + ``` |
| 75 | + </TabItem> |
| 76 | + <TabItem value="arrow" label="Arrow Function"> |
| 77 | + ```js |
| 78 | + const multiply = (x, y) => x * y; |
| 79 | + ``` |
| 80 | + |
| 81 | + <em>(If it's just one line, you don't even need the "return" keyword!)</em> |
| 82 | + |
| 83 | + </TabItem> |
| 84 | +</Tabs> |
| 85 | + |
| 86 | +## Practice: The "Tip Calculator" |
| 87 | + |
| 88 | +Try building this reusable tool in your `script.js`: |
| 89 | + |
| 90 | +```javascript |
| 91 | +function calculateTip(billAmount, tipPercentage) { |
| 92 | + let tip = billAmount * (tipPercentage / 100); |
| 93 | + let total = billAmount + tip; |
| 94 | + return "Your total bill is: " + total; |
| 95 | +} |
| 96 | + |
| 97 | +// Testing our machine |
| 98 | +console.log(calculateTip(100, 15)); // Output: Your total bill is: 115 |
| 99 | +console.log(calculateTip(250, 10)); // Output: Your total bill is: 275 |
| 100 | +``` |
| 101 | + |
| 102 | +:::info Keep it Simple |
| 103 | +A good function should do **one thing** and do it well. If your function is doing 5 different tasks, it's better to break it into 5 smaller functions! |
| 104 | +::: |
0 commit comments