Javascript Functions

Javascript Functions

Functions and their types

Introduction:

To perform any action we used to write the same block of code again and again. But Function came into picture💯. With a function, we can write a single block of code at once and call it whenever required. To understand more about functions and what is calling of a function let's take a look at a simple example.

Example:

function sum(); //function declaration
{

let m = 30;

let n = 20;

console.log("add: " ,m+n);

}
sum();  //calling of a function

//o/p: 50

The function is declared using the 'function' keyword with the variable name and '()'.

To use a function, we will first have to call that function to perform the defined task. We can call the function using: function_name()

What are Function Parameters:

Function parameters are the names listed in the function definition.

Function with Return Type

eg.

function sum (val1,val2); //function declaration
{ 
let result = val1+val2; 
return result; 
} 
let add =sum(10,25); //function calling
console.log(add);

now you might be wondering why did we assign a variable to the function call

Here's Why:

We send 10 and 25 to (func declaration line) So we created val1 and val2 arguments which were able to store these two values. Now when the function is returning something (result) Does the normal sum(10,25) has the capacity to store it? No. Sum needs something to store So we write add variable.

//program continuation
let add1 = sum(11,33)
console.log(add1);

So each time when we will call the function we must have somebody who can store the value that's why we used let add1=

String

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

This function prints the full url. This function has 2 things url and domain. Here we passed 2 strings and also added the third string i.e. con.

Here we can also obtain the output without adding the extra variable 'var' by directly writing console.log(URL('rudrakshi'+'.me')). By this, we can directly get our output.

Array

After learning the functions you may ask what if somebody wants to pass n no. of parameters. So let's make something that can enter n number of parameters.

example: Let's take a sumarray with 6 values in it. So if we are giving an array of 6 values It will be hectic to paas 6 variable names as arguments. Let's take a look at this example.🧐

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,4,5,6];
let ar_result=sum(sumarray);
console.log(ar_result)

We wanted to add the values of sumarray. So we created an element let sum=0 Then we looped over all the elements. arr[i] the i gives index number of array and we obtain the value assigned to the index. eg. arr[0] will be 1 and it will go on and get added to the sum variable.

Multiple Values

What if we do not want to pass any arguments So here is the example.✍️

function m(){
//Default Arguments
let sum=0;
for( let i=0;i<arguments.length;i++){
sum=sum+arguments[i];
}
return sum;
}
console.log(m(1,2,1,2,1,2)); //9

The function by default takes an argument. It is predefined. We generally do not prefer this type of code. We use something called Spread and rest.

Arrow Functions

Arrow functions are widely used in JavaScript. An arrow function is a shorthand syntax for defining functions.

const add = (a, b) => {
  return a + b;
};

In the above example, add is an arrow function that takes two parameters a and b. The arrow => separates the function parameters from the function body.

Finally, We now know a lot about functions and how they are declared.📚 Now next up we will learn what are objects. Till then

Happy Learning!😉