Function in JavaScript

Function in JavaScript

Why we use function?

The main objective of function is to avoid the repeatation of code.

Example: If we have to perform different operations on two variables we write code as follows:

let a=25
let b=5
console.log(a + b)
console.log(a - b)
console.log(a * b)
console.log(a / b)
console.log(a % b)

But what if we want to perform these same operation on other two variables?

Again we have to declare two variables and we have to write again same five lines for these new variables.

So, what is solution to minimize and to avoid repeatation of same code again and again?

Answer is "function".

Lets understand the above example by using function.

function calculator(a,b){     
console.log(a+b)
console.log(a-b)
console.log(a*b)
console.log(a/b)
console.log(a%b)
}
calculator(25,5)     //call the function by its name, here 25 goes into a and 5 goes into b 
calculator(20,10)

In above example:

  • function = keyword
  • calculator = function name
  • () = parentheses
  • a , b = parameters
  • {} = blocks
  • lines inside the {} = statements
  • value passed to function = arguments

So advantages of using function are:

  • No need to repeat same code

  • Different values can be passed to perform the same operation by simply two steps

    1. Call the function by its name
    2. Pass the arguments

Different ways of writing the function in JavaScript

1) Function Declaration: The syntax is as follows

function addition(a,b){ 
console.log(a+b)
}
addition(10,20)               //30

2)Function Expression: The syntax is as follows

let addition=function(a,b){ 
console.log(a+b)
}
addition(10,20) // 30

This is called as function expression because the expression: " function(a,b){ console.log(a+b) } addition(10,20) // 30 " is stored in variable 'addition'.

Note : Function expression is called as 'First class function' in JavaScript.

3)Arrow Function: The syntax is as follows

let addition=(a,b)= >{ 
return a+b
}
let sum=addition(10,20)  
console.log(sum)       //30

Note : Syntax advantage of using arrow function is that if there is only one statement returning some value we can write the above program as follows

let addition=(a,b)= >a+b
let sum=addition(10,20)
console.log(sum)          //30

Different types of functions

1)Function without parameters without return type:

function add(){
console.log(8+2)    
}
add()         //10
add()         //10

In above example the function 'add' has no parameters so no value can be passed to it. This function will give the same output every time we call it. We can not store and reuse the output as this type of function do not return any value.

2)Function with parameters without return type:

function add(x, y){
console.log(x + y)
}
add(20,10)     //30

Each time we call the function and pass different arguments to it, this type of function will give different output but we can not store and reuse the output because this type of function do not return the value.

3)Function with parameter with return type:

function add(x, y){
return x+y
}
let sum=add(10,20)
console.log(sum)           //30
console.log(sum+100)   //130
console.log(sum*10)     //300

In above example, the function return the value 30 (x+y) , the return value can be stored in a variable 'sum'. We can reuse this value for other operations which are shown in last two lines of code.

4) Function without parameter with return type:

function add(){
return (10+20)
}
let a=add()
console.log(a)   //30
console.log(a+10)  //40

This type of function return the value so the value can be stored and reused.