Functions

Functions

Table of contents

No heading

No headings in the article.

What are functions ?

A line code that is used numerous times, in a program. The reused line of code is written inside the function which is then called and this block code written inside the function will process the input and give the required out which the function was meant and that we can say a function can receive data and return data as well.

when we have to use the function then we use the term called Calling, Running or Invoking the function.

How to do Calling, Running or Invoking function?


// Anonymous Function ----->
function logger(){
    console.log("Enter your name :");

}

// calling the function--->
logger();

In the above code example, it can see a block of code is written inside the function block with the function name logger, when we have to call the particular named function we just have to type it as logger(). If the particular function you wanted to check in the browser console then uses this `console.log(logger()); it will get logged to the console.

What are Anonymous functions ?

This type of function does not take any value, instead, it returns a value.

Example:


// Anonymous Function ----->
function logger(){
    console.log("Enter your name :");

}

// calling the function--->
logger();

Functions not only return value, but they can also take a value and process it and then the result can be outputted. Functions should have a name, which will help to identify what this function is made up for. It takes value through parameters and finally the actual part of the code that will process the input should write inside the curly brackets, {..}.

What are parameters ?

Parameters are a part of the function that is written inside the parenthesis and separated by commas. The number of parameters you want depends on how many values you want to pass through the function at once. Let's see an example of how to pass value through parameters.

Example: Using single parameters.

function sumPolygon(q){
  return (q-2)*180;
}

console.log(sumPolygon(6));

Example: Using more than one parameter.

function points(twoPointer,threePointer){
  return (twoPointer*2)+(threePointer*3);
}

console.log(points);

What are function Declaration and function Expression?

function declaration benefit is, they can be called before they are defined in code.

Example: function Declaration.

function sumPolygon(q){
  return (q-2)*180;
}

console.log(sumPolygon(6));

The function is value, it can be stored in variables.

function expression it is not going to work , if we call first and define later.

Example: function Expression.

const sumPolygon=function(q){
  return (q-2)*180;
}

console.log(sumPolygon(6));

What is Arrow function?

The arrow function is the alternative to traditionally written functions, it is shorter than the traditional one. It can be used when writing shorter codes. when writing a traditional function certain things have to keep in mind -> function word will be removed with =>, braces { } and return will be removed and also the argument parentheses ( ).

Let's compare the writing of function in both ways traditionally and with the Arrow function.

Example: Traditional way of writing a function:-

function calcAdd(q){
  return q +100;
}

console.log(calcAdd(2));

Example: Above function can be written using the Arrow function:-

const calcAdd =  q =>  q + 100;

console.log(calcAdd(2));

You will encounter an error if you call the function before defining it.

Error: If you do this:-

console.log(calcAdd(2));
const calcAdd=q=>q+100;

Output:-

Uncaught ReferenceError: Cannot access 'calcAdd' before initialization

*This type of error does not occur when you write function declaration.

How to call function inside a function?


function valueDividedBy100(p){
  return p/100;
}


  function  percentChange(q,p){
    const a=valueDividedBy100(p);
    console.log(`The value ${p} is converted multiplying with 100 to produce ${a}`);
    return `when ${q} is multiplied with ${a} it producess ${q*a}`
  }


  console.log(
    percentChange(50,25));