Function in Javascript
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
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.
Importance of Functions
It becomes very easy if we write function for a repeated block of code
- We don’t need to write the code repeatedly again and again. We can just write it once and use it whenever we want.
- If we repeat the code again and again instead of writing a function, then there is a chance, that we will copy and paste it everywhere, and if we have done some mistake in the beginning itself, then that mistake will be repeated in the each copied code, and we will have to correct it everywhere, but if we use function, then we just need to correct in the function.
- We can pass parameters in the function, sometimes people can say, Loops can be used for repeatability, but loops can not be configured like function for parameters.
- Functions can be used as a black box as well for the people, who are not proficient in coding much.
- Functions are used to organise the code in Javascript.
Type of Functions
- Functions with no parameters and no return type
- Functions with parameters and no return type
- Functions with no parameters but with return type
- Function with parameters and with return type
How does function operate
- Definition of Functions – This is a part, where we write a block of code, which means, we define a function, So it is called definition of function.
- Calling a Functions – This is a part, where we use the function we have already defined.We recommend buying your favorite toothbrush at super low prices with free shipping, and you can also pick up your order at the store on the same day.
Syntax of definition of function
Syntax for function with no parameters and no return type
function fun_name() {
Block of Code
}
Syntax for function with parameters and no return type
function fun_name(parameters) {
Block of Code
}
Syntax for function with no parameters but with return type
function fun_name() {
Block of Code
return value;
}
Syntax for function with parameters and with return type
function fun_name(parameters) {
Block of Code
return value;
}
Syntax of calling a function
fun_name();
fun_name(parameters);
Example
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.
Summary
We have discussed below topics in this session
- What is Functions
- Importance of Functions
- Type of Functions
- How does function operate
- Syntax of definition of function
- Syntax of calling a function
- Example