Functions in Java Script

Functions in Java Script

Functions :

A function means a block of code is first created and whenever the block of code is required it will be called. To avoid repeating the same code again and again all over places, we can use a function to wrap that code and reuse it. It helps you to divide a large program into small and manageable functions.

Declaration Of Function :

To declare a function, you use the "function" keyword, followed by the function name, a list of parameters, and the function body.

Syntax :

function details ()
{}

As in the syntax declaration, we can define it by using the function keyword, followed by a function name, open and close parenthesis, and curly braces.

Now let's understand this with a small example :

function sum() 
{
    let num1=20;
    let num2=30;
    console.log('Addtion of numbers is :', num1 + num2);
}
sum();

We started with the function keyword. This is how we declare a function. Then, we defined a function name, which is sum. Then, we added parenthesis means (). We use parenthesis to add parameters, which we will be exploring more later in this post. Then, we used curly braces to create a function body. All the code that is within these curly braces is called the function body. And it's this code that will be executed when we run this function. Here in this case two variables are declared num1 and num2 with values 20 and 30 respectively, and then the two functions are added. To use this function, we simply write the function name followed by a parenthesis. And this process is called "invoking", "running", or "calling" the function. Here in this case it is sum(). The Output of the above function is :

Addtion of numbers is : 50

Function with Parameters :

Parameters :

It is a name or variable that is declared in the function definition.

Let us understand this with an example :

function sum(value1,value2){
    let num1=30;
    let num2=60;
    console.log('Addtion of two numbers is', value1+ value2);
}
sum(30,60);
sum(50,120);
sum(100,100);

Here in this value1 and value2 are the parameters. First, the functions is being called with the set variables which are 30 and 60 and they are stored in the respective variables num1 and num2. Now the function is being called 3 times and the result is displayed with the desired values. It is the advantage of declaring a function with parameters. The Output of the above is :

Addtion of two numbers is 90
Addtion of two numbers is 170
Addtion of two numbers is 200

Function with return type :

With the return keyword, we can return any value from the function.

Let us understand this with an example :

Function for Addition :

function sum(val1,val2)
{
    let result=val1 + val2;

    return result;
}
let add=sum(20,30);
console.log('The result of addtion is',add);

Here in this case first a function is declared with name sum and with two parameters val1 and val2. Here result stores the addition of val1 and val2 then by the return the result is going towards the variable add which stores the values 20 and 30.

So we see by the function with the return we can return any value from the function.

The Output of the above function is :

The result of addtion is 50

Now Different functions are created for subtraction, multiplication, division and modulus.

Function for Subtraction :

function minus(val1,val2)
{
    let result=val1 - val2;
    return result;
}
let sub=minus(30,20);
console.log('The result of subtraction is',sub);

The Output of the above function is :

The result of subtraction is 10

Function for Multiplication :

function mul(val1,val2)
{
    let result=val1 * val2;
    return result;
}
let multiply=mul(30,20);
console.log('The result of multiplication is',multiply);

The Output of the above function is :

The result of multiplication is 600

Function for Division :

function division(val1,val2)
{
    let result=val1 / val2;
    return result;
}
let div=division(30,20);
console.log('The result of division is',div);

The Output of the above function is :

The result of division is 1.5

Function for Modulus :

function modulus(val1,val2)
{
    let result=val1 % val2;
    return result;
}
let mod=modulus(30,20);
console.log('The result of modulus is',mod);

The Output of the above function is :

The result of modulus is 10

Function with String :

function URL(url,domain)
{
    let con="https://";
    let result=con + url + domain;
    return result;
}
let google=URL('google','.co.in');
console.log(google);

Here function named URL is created which contains two parameters URL and domain. It gives the output of the search engine google.co.in

https://www.google.co.in

Function which calculates values inside array :

function sum(arr)
{
    let sum=0;
    for(let i=0;i<arr.length;i++){
        sum=sum +arr[i];
    }
    return sum;
}
let sumarray=[1,2,3,5,6,7,8,9];
let arresult=sum(sumarray);
console.log(arresult);

Here, in this case, it does the addition of values inside the array. First of all, the function is declared with the name sum then the parameter named arr is passed then the variable named sum is declared. Then with the help of for loop calculates the values inside the array. The Output of the above is :

41

Arrow Functions :

There is another way of declaring functions in modern JavaScript, and that's with the arrow function, which looks like an arrow: () => {}.

They are often used for short lines of code or to execute single liners functions.

Arrow functions are shorter and faster to write.

Syntax :

(parameters) => { statements// code to be executed };

Let us create a simple arrow function to understand :

let kapil=()=> {
    console.log('My name is Kapil Sarkar');
}
kapil();
  • First of all, a variable is declared as named kapil.

  • Then ()=> is added to it so then it can be called an arrow function.

  • Inside the function, a simple statement is printed.

  • Then the function is called.

The Output is :

My name is Kapil Sarkar

Arrow Functions with parameters :

let sum=(var1,var2)=>
 {
    let add=var1 + var2;
    let sub=var1 - var2;
    console.log('Addition of the numbers are :',add);
    console.log('Subtraction of the numbers are :',sub);

 }
 sum(10,20);
 sum(30,60);
 sum(200,100);

Here first a variable is declared named sum then ()=> is added to make it arrow function.

Inside the sum, two parameters are passed var1 and var2.

A variable named add is declared which does the job of addition of var1 and var2.

A variable named sub is declared which does the job of subtraction of var1 and var2.

Then the variable add and sub are printed.

At last, the function sum is called with two values 10 and 20.

The Output of the above is :

Addition of the numbers are : 30
Subtraction of the numbers are : -10
Addition of the numbers are : 90
Subtraction of the numbers are : -30
Addition of the numbers are : 300
Subtraction of the numbers are : 100