|
| 1 | +## ⚑ Functions |
| 2 | +Functions are reusable blocks of code that perform specific tasks. It helps to organize the code, make it more modular, and for code reusability. |
| 3 | + |
| 4 | +### ☴ Overview: |
| 5 | +1. [Defining functions](#-defining-functions) |
| 6 | +2. [Calling functions](#-calling-functions) |
| 7 | +3. [Function arguments and return values](#-function-arguments-and-return-values) |
| 8 | +4. [Function Scope and closures](#-function-scope-and-closures) |
| 9 | +5. [Arrow functions](#-arrow-functions) |
| 10 | + |
| 11 | +### ✦ Defining Functions: |
| 12 | +The functions are usable before defining it. |
| 13 | + |
| 14 | +*Syntax:* |
| 15 | +```javascript |
| 16 | +function functionName(parameter1, parameter2, ...) { |
| 17 | + // Function body (statements to be executed) |
| 18 | + return value; // Optional: Returns a value |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +```javascript |
| 23 | +function greet(name) { |
| 24 | + console.log("Hello, " + name + "!"); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### ✦ Calling Functions: |
| 29 | +To use function in the JavaScript code, that need to be called where it is required. |
| 30 | + |
| 31 | +*Syntax:* |
| 32 | +```javascript |
| 33 | +functionName(argument1, argument2, ...); |
| 34 | +``` |
| 35 | + |
| 36 | +```javascript |
| 37 | +greet("Kumar"); // Output: Hello, Kumar! |
| 38 | +``` |
| 39 | + |
| 40 | +### ✦ Function Arguments and Return Values: |
| 41 | +**Arguments:** Values passed to a function that accepts as parameter of it when it's called. |
| 42 | +**Return Values:** Values that a function can return to the where it was called or invoked using the return statement in the function. |
| 43 | + |
| 44 | +```javascript |
| 45 | +function add(a, b) { |
| 46 | + return a + b; |
| 47 | +} |
| 48 | + |
| 49 | +let sum = add(1, 2); |
| 50 | +console.log(sum); // Output: 3 |
| 51 | +``` |
| 52 | + |
| 53 | +### ✦ Function Scope and Closures: |
| 54 | +**Scope:** The region of code where a variable or function is accessible. |
| 55 | +**Closures:** Functions that have access to variables from the outer scope, even after the outer scope has finished executing. |
| 56 | + |
| 57 | +```javascript |
| 58 | +function outerFunction() { |
| 59 | + let x = 10; |
| 60 | + |
| 61 | + function innerFunction() { |
| 62 | + console.log(x); // Accesses the outer variable x |
| 63 | + } |
| 64 | + |
| 65 | + return innerFunction; |
| 66 | +} |
| 67 | + |
| 68 | +let closure = outerFunction(); |
| 69 | +closure(); // Output: 10 |
| 70 | +``` |
| 71 | + |
| 72 | +### ✦ Arrow Functions: |
| 73 | +It is defined in a concise way or in shorthand form. |
| 74 | + |
| 75 | +*Syntax:* |
| 76 | +```JavaScript |
| 77 | +(parameters) => expression; |
| 78 | +``` |
| 79 | + |
| 80 | +```javascript |
| 81 | +let greet = (name) => { |
| 82 | + console.log("Hello, " + name + "!"); |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | +[⇪ To Top](#-functions) |
| 88 | + |
| 89 | +[❮ Previous Topic](./control-flow.md)   [Next Topic ❯](./dom-manipulation.md) |
| 90 | + |
| 91 | +[⌂ Goto Home Page](../README.md) |
0 commit comments