# How functions work in JavaScript🍺

### Sai gowtham ・2 min read

Functions are key to the programming and we can reuse the functions

So let's talk about how functions work in JavaScript.

Every function in JavaScript starts with a **keyword** function.

Let's write our first function we already know every function starts

with a keyword function.

## Function Declaration👷🏼♀️

```
function add(a,b){
return a+b;
}
```

We called it as function defintion or function statements/declaration

What above function does is a function with a name add takes

two parameters and returns the addition of two parameters.

So By defining a function cant do any work to make functions work we need to invoke it/calling a function is called function **invocation**.

How we call a function in JavaScript they are 4 different ways to call a function Currently, I'm showing only one way.Becuase other 3 is something confusing I will explain them in my next post.

## Function Invocation 👮🏼

```
add(1,2) //3
```

we are passing two arguments to the function which are 1,2 so we get an output of 1 and 2 which is 3.

What the hell arguments/parameters have you confused why I'm calling in different names in the same function.

Lets clear confusion now.

**Parameters**: which are passing to the function at the time of declaration.

Parameters are named variables which are a,b in our add function.

**Arguments**: Which are passing to the function at the time of invocation.Arguments are the values which are 1,2 in our add function.

What return does to our function is it necessary😛.

**return** is a keyword it helps to stop✋ the running function.

**Example**

```
function printNumbers(n){
if(n === 1){
return 1;
}
if(n === 2){
return 2;
}
}
```

In above example, we are stopping the function if n is 1/n is 2.

so we are stooping function at 2 different times.

if n is 1 then we never reach the line 3 because we are using return at

line 2.so our function stops running🤙 at line 2 and gives output 1.

**Hope you guys love these Like and share your thoughts about confusing
things in functions.**

**Check out My other interesting Posts**

How loops work in JavaScript

Great ArticleThanks...Some functions can also be created without the

`function`

keyword like this:I'm not talking about function expressions.