Before understanding function in Javascript, Let’s take an example of Test Case, where we have to add 2 numbers multiple times, then what can we do, You might say that we can write the code to add 2 numbers that many times or we can use loops, and Yes, We can do that but we have a better alternative as well, and that alternative is function. How is function a better alternative of loops, We will see that in this session, but let’s understand first, what is a function.
What is Functions, So, as the name says, function, it means does something. How does a function do something. So, function is a piece of code, that means, function is a program, and it does, what we have written inside it. So, if we have written a function for adding 2 number, it is going to add 2 numbers, If we have written a function for subtracting 2 numbers, then it is going to subtract 2 numbers. So, function is a piece of code or program which is written seperately, and is reused whenever needed.
Now, We get a question, that why do we need a function so, there are some benefits of using function. Let’s see the benefits and importance of a function.
It becomes very easy if we write function for a repeated block of code
function fun_name() {
Block of Code
}
function fun_name(parameters) {
Block of Code
}
function fun_name() {
Block of Code
return value;
}
function fun_name(parameters) {
Block of Code
return value;
}
fun_name();
fun_name(parameters);
Now, Let’s say, I have to add 2 numbers 5 times, then we can write the code for that something like this.
console.log(2+3);
console.log(2+3);
console.log(2+3);
console.log(2+3);
console.log(2+3);
Now, it doesn’t make sense writing this program like this because we might have to write this 100 time or 1000 times also, So, It is better, we must use loops, So, if we use loops for writing this program, then we are going to write the program something like this.
for(let 1=0; i<5; i++) {
console.log(2+3);
}
Now, this looks good, but we have a better alternative in the form of function, let’s see if we have to write this code using function, then how are we going to write it.
function add() {
console.log(2+3);
}
Now if we want to call this function in my program, then I can call it multiple times. Something like this
add();
add();
add();
add();
add();
Now, If I want, that I don’t want to add 2+3, Now, I want to add 3+4, then I can just go to the function, and change the code there, and I don’t need to anything anywhere else.
add() {
console.log(3+4);
}
So, We can say that this function resolve our problem code repeatability, and code modifying, if we want to modify it later.
Now, We might get a question, that if the use of function is just to resolve the problen of code repeatability, then why don;t we use loops, Why did we use function, So the answer is parameters.
Now, the function is always printing addition of 3 and 4 which is 7, but what if I want to add different numbers every time and want to print the addition of that number, and loops can not do that and that’s why we use functions with parameters. let’s see how function with parameters work.
function add(x, y) {
Console.log(x+y);
}
Now, let’s call this function
add(2, 3);
add(3, 3);
add(5, 1);
add(8, 8);
add(2, 7);
We can write it with return type as well
function add(x, y) {
return x+y;
}
If we want to call parameterized function with return type, we can do something like this.
Console.log(add(2, 3));
Console.log(add(3, 4));
Console.log(add(5, 7));
Console.log(add(6, 3));
Console.log(add(9, 5));
We will discuss about Arrow functions in the next session.
We have discussed below topics in this session
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…