Functions
Tags: javascript, beginners, programming, tutorial
Published: 2022-11-09 22:50:57

Hello my naive friend who thought this was a safe place for people that dislike functions. Functions spread like covid so they got their way to the world of programming aswell. Let's take a look at them.


**What is a function?** 
Well, think of it as a machine that will do a its thing when given a coin. Our machine is the function itself, the coin is "calling" it. Every time we call the function it will do some task we assigned it to. Those tasks are called arguments. They can be anything. We can make a function that will print "you're gay" each time we call it or a function that will calculate the product of two numbers. 

_Here are the examples_

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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wgovvanhhnak747o40a.png)


Now, what can we do with functions? Well, quite a lot. 
Let's look at the easiest one


  

1 . Return

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


  

2 . Booleans

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


   

3 . If 

Sometimes we need to let the function do more than one thing under certain conditions. Here's when we use IF

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

As you can see, we gave it two conditions, and for each one it did different things

   


4 . Equality Operations

With IF we can check equality of pieces of data


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

Here we were able to check when something is equal to 23 or not. We can also use this method to find if the value is greater or smaller than our number (">" strictly greater, "<" strictly smaller, ">=" greater or equal, "<=" smaller or equal)




5 . AND and OR operations

We can also command the function to do its task only when both conditions are met. That's AND operation

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

The OR operation, on the other hand, just needs to meet one condition to be true

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




6 . Else

Now when our conditions for IF are not met, we can tell the function to do something else

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

We can also have "else if" which will allow us to have more than two conditions

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





**Also**, when talking about functions we need to remember scopes.
Those are the domains of visibility of variables
1 . Global Scope

Just a variable that's declared outside of a function. Is seen everywhere

2 . Local Scope

A variable that's declared inside of a function, therefore is only seen there


That's all for today!!
Senk yuuuu!!'


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