# Function In  Javascript:

The function is a block of code designed to perform some particular task

```javascript
function myFunction(p1, p2) {
  return p1 * p2;
}    
```

### Anonymous functions:

In this, we can declare a function in the form of a variable, and hence can remove the name of function after function keyword

```javascript
var f = function(a, b) {
  const sum = a + b;
  return sum;
}
console.log(f(3, 4));    
```

### Immediately invoked function :

You can create a function and call immediately call the function after declaring it.

```javascript
const result = (function(a, b) {
  const sum = a + b;
  return sum;
})(3, 4);
console.log(result); 
```

### **Functions Within Functions:**

Here you can create nested functions inside a main function and could even return them

```javascript
function createFunction() {
  function f(a, b) {
    const sum = a + b;
    return sum;
  }
  return f;
}
const f = createFunction();
console.log(f(3, 4));
```

### **Function Hoisting:**

Javascript has a feature of hoisting where a function can sometimes be used before it is initialized. But it is considered as bad practice as it reduces code readability.

```javascript
function createFunction() {
  return f;
  function f(a, b) {
    const sum = a + b;
    return sum;
  }
}
const f = createFunction();
console.log(f(3, 4));
```

### Arrow functions:

It is a very common way to declare a function, here there is no need to write a function keyword.

```javascript
const f = (a, b) => {
  const sum = a + b;
  return sum;
};
console.log(f(3, 4)); 
```
