Functions in Java Script
Tags: functions, javascript, nestedfunction, definingfunctions
Published: 2022-11-09 20:49:04

Let's start with the general understanding.

**What is a function in general?**
A function is a group of code that performs a specific task. When you define a function, you can call it everywhere and do a particular action.

**Why do people create functions?**

1) Functions Encapsulate Tasks - often functions named "black boxes" because when we call them, we do not care what the inner code of it is. We need just its result.

2)Functions Separate Tasks - programmers can cleanly divide their code into segments to work with it efficiently.

3)Functions Let You Reuse Code - often, we reuse the same code multiple times, and not to repeat ourselves, we can write functions once and call them when needed.  

4)Functions Make Testing and Debugging Easier - often, we make mistakes in our code, and it is easier to correct errors in one place and make sure it works appropriately and then call when needed.

## Java Script functions

In JavaScript function is a block of code to perform a task, and we can use it when we call it and input arguments if needed.

##Function Declaration

To create a function, we have to.

1) **write "function" keyword** 

2) **give a name to our function**
     naming a function has the same rules as naming variables.

     a)Names can contain letters, digits, underscores, and 
     dollar signs.

     b)Names must begin with a letter.

     c)Names can also begin with '$' and '_.'

3) **write parenthesis "()"**
4) **open curly brackets "{}"**
5) **write the logic of a code**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/we52mzaxqbxzfeqt8tot.jpeg)
6)**return object which we want to get as output from the function**
When the code reaches the command 'return,' it automatically stops execution and returns the variable's value.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf5fzwe4udadg8y0h9bu.jpeg)



Functions can also be defined as regular variables 
1) keyword 'let,' 'var,' or 'const.'
2) function's name
3) equal sign '='
4) function()
5) curly brackets '{}.'
6) logic of the function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s67uxfp7mhmkhnd4k3b8.jpeg)
##Function Invocation
We write functions, but we have to call them with arguments (if needed) to get the value from the function.

For calling.
1) we write the function's name
2)parenthesis "()"
3)in parenthesis, we need to write arguments(if needed)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdhxj6fjbpgccgb8z3vd.jpeg)

The "()" Operator Invokes the Function. If we call the function without it, JS will return the function itself

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpjlj7fl8d37z21lfj8k.jpeg)


## Function Scope

When we define variables inside the function (it does not matter with var, let, or const), variables are  accessible only inside the function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bluuslf2ktidlmdl0mt.jpeg)

**Global variables**
Variables that are defined outside of any function become global and can be accessed by any function 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h11sg5rjayyiti67tis0.jpeg)

**Function arguments**

Function arguments can be used inside the function and are local to it.

**Checking for variables**

Function first searches the logic values inside the function's scope and uses them. If the function does not find, it goes up from its scope and uses the first value of the particular variable.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxitkokfy0txgcojc6vp.jpeg)

## Nested functions
 Functions have access to the global scope, and we can use functions to plug in other ones.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/id29ohfqdisz04ii20fz.jpeg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3heymfrgtasyllm7oue.jpeg)