DEV Community

Cover image for Functions in JavaScript
Levon Yedigaryan
Levon Yedigaryan

Posted on

Functions in JavaScript

In JavaScript, there is something called functions. In this post, I will explain what that "thing" does, how to use it, and why we need it.

What are Functions?

Functions are separate pieces of code inside the code used to do some operations. Functions are often used to avoid unnecessary repetitions inside the code.

For example, if we want to do some complicated mathematical computations 100 times, we don't have to write the code 100 times. In this case, all we need is 1 function that will contain the long code; after that, we simply use that function as many times as we wish.

How to Declare a Function?

To declare a function in JavaScript, we have to write.

function Name of the function (variables or parameters which are passed to the function)
{
The code which the function will
execute

}

For Example

function sum(a, b)
{
console.log(a+b)
}

Don't worry. I am going to explain what each of these means.

Variables or Parameters of the Function

The function might use parameters both from its scope and global scope. Still, whenever there are 2 variables with the same name in the function and in the global scope, the function will take the value of the one inside itself (for more about scopes and variables, check my post about variables).

So the function will recognize the variable declared outside of its body if and only if there is no other variable inside it with the same name.

Functions may receive some values from the outside (These are the values we put inside the () when we declare the function)

function sum(a, b)
{
console.log(a+b)
}

This means that whenever we use the function, we will give it 2 values which, inside the function, will be named a and b. So after this, it will know their values whenever the function finds a or b inside of itself.

So, How do We Use the Functions?

Programmers often use another term for "use"; instead, they say "call the function." Simply declaring the function is not enough; we must call it at some point. To do it, we write the function's name, put (), and, if the function requires, inside () write the values we want to pass to the function.

function sum(a, b)
{
console.log(a+b)
}
sum(5,6)

This will print 11.

From now on, we can call the function as many times as we wish, with any variables we want to.

function sum(a, b)
{
console.log(a+b)
}
sum(5,6)
sum(5,5)
sum(9,4)
sum(10,150)
sum(0,1)

This will print
11
10
13
160
1

Return

Some functions may return the values, but what does that mean?

Before understanding this, we must learn that functions can also be assigned to variables. If we Write

function sum(a, b)
{
debug.log(a+b)
}
var c = sum(5,6)

The function sum() will be called as c is declared, and c will get the value of sum(). The problem is that c is not equal to 11, as the function only prints the sum but does not send its value to the variable c. To send the value, we use return. When we write

return The value we want to send

Inside the function, it will send the value to the variable to which the function was assigned to.

function sum(a, b)
{
return a+b
}
var c = sum(5,6)
//Now c is 11

Note that after reaching the line with the return, the function stops until being called once again.

I hope you enjoyed this post, and I wish you good luck in your programming job ;)

Top comments (0)