Full transcript of “Functions in JavaScript | JavaScript Made Easy - Session 19”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 Hello all, welcome to an another session in this course. As part of this session, I'm going to cover the next topic in this JavaScript that is functions. So let's get started. So what is the topic? JavaScript functions. Okay, or functions in JavaScript. The topic is functions in JavaScript language. So what exactly are these functions? What exactly are these functions? What exactly are these functions? Function is nothing but Okay, function is nothing but a block of code. Okay, block of code which can perform
0:48 some task. Okay, or to perform some task otherwise you can say function is nothing but a block of code. Okay. To perform some task. To perform some task. This is what is a function. Okay. This in in this function we can put some block of code which can do some task. Okay. That's what is a function. That's what is a function. What is the purpose of the functions and all? I'll cover later. Okay. At this point of time I'm only explaining what exactly is a function. But why we need functions and all? I didn't cover it. I'm just explaining what exactly is a function. Function is nothing but a block of code. The syntax of the function will be something like this. There is a keyword known as function keyword. Okay, in JavaScript, if you want to create a function, you have to use this keyword known as function keyword followed by
1:34 the name of the function. Okay, let's say sample fun. Okay, sample fun is a name of the function and you have to give this circular brackets and this curly brace for start of the function. Here curly brace for ending of the function. In between this starting of the function and this is starting of the function guys. the starting curly brace and this is ending of the function and this is a function function name of the function is sample fun which should be followed by circular brackets. Here you can write any number of statements guys. For example, this is statement one statement uh statement one. Okay, statement two code statement two. Then this is a statement uh
2:20 statement three and so on. Okay, it can have any number of lines of code. Okay, what is a function guys? Function is nothing but this function is nothing but a block of code. Is this a block of code? Yes or no? Is this particular thing that I have selected here in this particular function thing is nothing but a block of code. Okay, statement one, statement two, statement three lines of code is nothing but block of code. Okay, a block of code is nothing but a function. Okay, and this is a keyword which is required in JavaScript. In order to create the function, if you want to create a function in JavaScript, you have to use this function keyword followed by the function name and give the circular brackets and starting of the function, ending of the function and this block uh this function will have some block of code. Okay, so this what is exactly is a function guys. Okay, so what is a function? Function is nothing but a block of code to perform. Okay, using
3:06 this block of code, some task can be performed. Okay, using this group of code or block of code, some task will be done by the function. Okay, so in that cases, we'll be generally creating a function guys. Okay, so but what is the purpose of the function? I'll cover later. For now, understand what is a function. Function is nothing but a block of code to perform some task. Okay. When you run this function, some task will be performed by this function. Okay. To create the function, we need the function keyword followed by the function name. That's it. Okay. So now let's uh understand about the functions in a detailed manner guys. Okay. So I'll I'll just open these notes guys just for reference I have these notes with me. Okay. Without these notes also I can explain but you know I I would like to keep this particular notes as a reference. So these are the notes. What is a function guys? Function in JavaScript is
3:51 nothing but a block of code which is created. Okay, this function is created to perform a specific task. To perform some task, function will be created and function will contain a block of code. Okay, function is nothing but a block of code. Okay, create created or designed. Okay, to perform some task. That's what is a function. Okay, don't worry about what is the purpose of the function and all those stuff we'll cover later. But for now, how to create a function? function keyword we have to use and name of the function followed by circular brackets starting of the function ending of the function and this particular thing is a block of code. Okay. So let me practically show you how to create the functions and how to work with the functions guys. Okay. Till now in till the previous sessions I have not created any function guys. Okay. Till now you see the code has been written directly under the JavaScript file. Let A is equal to 5, let B is equal to 4 and all
4:38 the statements are written directly. Right? So what I'll do here is I'll just delete this uh files. Okay. I don't need these files anymore. So let me delete all these JavaScript files which I have used in previous sessions for demonstration. Let me delete all those things and create a new Java uh new file known as fun one.js. Okay, here I want to I can write the code. Basically I can write the code but what I want to do is I want to create a function guys. Okay, how to create a function? Which keyword I have to use? Function keyword I have to use. Okay, there's a predefined keyword in JavaScript using which we can create the functions followed by this function keyword which JavaScript recognizes. We need to give the name of the function guys. Okay, name of the function. We can we have to give sample sample one is the name of the function. Let's say I'm giving the name of the
5:24 function as sample one and this function name should be followed by circular brackets. Okay, if I have to create this function name followed by circular brackets and this is starting of the function and this is starting of the function and this is ending of the function. This is starting of the function. This is the ending of the function. Function function function name circular brackets starting of the function and ending of the function. Starting of the function and ending of the function function function name circular brackets starting of the function and ending of the function. Now in this you can write any number of lines of code. It can be one line. It can be multiple lines doesn't matter guys. Okay. Let me write some print statements. Here you can write some real code but uh for now okay I'm writing only the print statements if required okay in the coming uh coming sections of this sessions right I'll be writing the real code but for now let me write the
6:10 print statement console.log log inside sample one okay function like this I'll write okay so like this if I write and here if I run this code that is node space name of the JavaScript file and extension fun 1js fun 1.js JS and press enter. What is happening? Did you get any output guys? Did you get any output? The code, the block of code which is there inside this function got executed or not when I have run this particular JavaScript file is any output I got. Node fun1.js any output I got guys. I didn't get any output guys. You see when I say console.log inside sample one function nothing got printed. This particular print statement didn't get executed. The reason behind that is you see if this
6:55 console.log log is okay it's not part of a function or if it is write directly written like this you see when I run this you see this print statement is getting executed but what if what if this particular print statement is part of the function this line won't be executed because the function is not getting executed how to run how to run this function code how to run this function code guys let me clear it okay here if I run this like this you see output is not coming but how to run this function code or the block of code which is there in this function. How to run? By default, the functions will not run. Okay, the block of code which is there inside the function will not run by default. If you really want to run, you need to call this method guys. How to call this method sample one and give the circular brackets and
7:42 put a semicolon. This is what is a function calling statement. What is this guys? This called as function function calling statement. If you don't call the function, the function code will not run. the function will not run the function or function the code inside the function won't run if you don't call it if you call it only the function and the code inside the function will run okay now let me clear this run this you see now the code inside the function when I'm calling this function okay when you are running this code when we when the when the execution reaches this fifth line sample one okay which is nothing but this part guys copy this function name and circular brackets and Just simply put a semicolon symbol to close a function. Okay. When you write
8:27 this line, this will call this particular function. When the function is called using this function calling statement, the code inside the function will be executed. That is inside sample one function will get printed here. Okay. Now is this particular function will run only one time. Can can be called only one time? Can I call this function only one time? No, you can call it any number of times. If I call the function again like sample one for second time, this this is another function calling statement. Okay. Similar to the first one, this is another function calling statement. Okay, no matter how many times I call this function like a sample one, sample one, sample one, sample one. Okay, clear. If I run this code, you see inside sample fun sample one function inside sample one function inside sample one function inside sample one function inside sample one function. Five times
9:12 the uh print statement got printed. Okay, when you're calling the function, every time you call the code inside the function got distributed and when the code inside the function get distributed here in this example, inside sample one statement will get printed. Okay, like an inside sample one, inside sample one, inside sample one, inside sample one, inside sample one will get printed. Okay, there is no rule that you need to call the function only once. You can call the same function any number of times. Okay, now let me clear this out. Now, what exact what what are the other things about the function here? Whatever the function I created, does it have any parameters here? Is it intaking any data here? No, circular brackets are empty. This function, okay, is not intaking any data because the circular brackets are empty. Uh, but I can put some parameters
10:00 inside the circular brackets. Okay, I can put some parameters. For that, I'll just, you know, let me also go through the reference just to cross check that nothing is going wrong, guys. Okay, just to cross check that nothing is going wrong or I'm not forgetting anything. I'll generally do like that. Okay. So this is normal one. Okay. This is a function calling statement. Okay. Now parameters only. Okay. The next one is a parameters. The next one is a parameters. Let me create another function. Fun to do. JS. So here I'm going to create another function guys. Okay. Here I'll be creating one more function that is fun. Function is a keyword followed by demo one. Demo one is a function name followed by circular brackets. Starting of the function ending. This is a curly braces starting of the function. Ending curly braces is the ending of the function. Function
10:45 function name circular bracket starting of the function and ending of the function. Now function function name circular brackets here. How many parameters are there? Zero parameters right here zero parameters are there. What if I write okay what if I give some name like you know uh name. Okay. If I give some parameter like name, this name that I have written here is one of the parameter. Okay, only one parameter is there inside this function that is a name. Okay, so here whenever you call this particular function, I want to do something like this. Okay, console.log welcome name. Okay, this place symbol already covered in the previous session, right? You know, string congratation. Okay, this here string lit
11:31 uh place symbol on the left side of the place symbol operent is string literal and the name is also a string name can be anything. Okay, welcome name means the resultant will be a string lit and this place will act like a concatenation operator instead of the addition operator. Now guys, now since earlier this particular function doesn't have any parameters. The circular brackets used to be empty in the previous example. But now this particular function has one parameter. How to call this function? If I run this code, nothing will be running guys. Okay. Okay. 12.js, nothing will get printed here. You see, because the the the block of code which is there inside the function will be executed only when you call the function. How to call the function using the function calling statement. How to call the function calling statement? Copy the name of the function here and put circular brackets and put a semicolon here. Now, will this
12:19 work? Will this work, guys? Let's try this out. Will this work? What is happening? Yes, it is working. It's calling the function. But what is the problem? It's calling the function. The function is getting exe the block of code inside the function is getting executed. But welcome space. What is coming here? In place of the name undefined is coming because you are not passing any data. Okay. To this particular parameter. The method is ready to intake the data. But in the method calling statement, you are not passing the data. Okay. This name is called as a parameter guys. Okay. This name is called as a parameter. Technically this name here one parameter is there inside this function declaration that is name is one parameter. And here if one parameter is there if you don't pass any data okay
13:05 JavaScript will assign undefined thing. Okay if you're not passing anything it will become undefined. Okay. What if I pass something like for example I will pass my name like string lit name I will pass. Okay. In this case what will happen? I run this code you see welcome space whatever the data I am passing to this parameter is getting printed here. Okay. So when you call this particular function this Arun motor will go and store into the name parameter and here you are inside the function you are utilizing the name parameter and here welcome space plus whatever the data that you have passed to the name parameter that is arun motor will be printed that's why welcome aron motor will be printed okay this is what happens guys this is what happens and this particular data okay that you are passing from the function calling statement is called as an argument okay argument it is an argument the data that
13:51 you are passing to function calling statement and that will go and store into the parameter of the function is called as an argument and this uh then uh the variable kind of thing that is provided in the circular brackets of the function declaration is called as a parameter and the data that you are passing to this parameter is called as an argument okay so whatever whenever I call this method uh sorry whenever I call this function whatever the argument I'll pass that will be taken and accordingly the function will work for example if I call this function again yes I can do and this time I'll pass. Okay. And here uh here another argument is there guys. Okay. A different argument I'm passing here compared to the first one. I'm calling the same function two times. So first welcome arun motor will be printed then welcome vandun should be printed. So let's see
14:37 what is happening. You see welcome arun motor and welcome vandunawad got printed. Okay. So this is what is a parameterized function. But here how many parameters are there in this parameterized function? How many parameters are there? Only one parameter is there. Is it that only one parameter should be there in functions? No, we can have any number of parameters guys. So I'll explain one more example here which has multiple parameters. So here only one parameter I have explained. Okay, but you can have any number of parameters guys. Okay, so let me create a function which has multiple parameters fun. JS and here I would like to create a function function add. Add is the name of the function. Here a, b starting of the function ending of the function. Here two parameters are there. Here what I'll write okay here I'll say
15:26 sum of a and sum is otherwise sum is plus circular bracket or you can write the logic also. Okay let c is equal to a + b. Okay or let sum is equal to let sum is equal to a + b. Here you can say plus sum. This particular v variable sum you have to sum is a uh you know sum is plus sum. Okay. Like this you can add. Now I can create one more function function function subtraction. Okay subtraction a comma b or x y. Anything you can give any names you can give. You can even give a a comma b also here. Not a
16:13 problem. And here. Okay. Uh here I'll write let result is equal to x - y. Result is equal to x - y. Okay. Console.log. Okay. Subtraction is plus result plus this particular variable result. Okay. Like this you can create any number of functions guys. You can create any number of functions. But how here how many parameters are there? Two parameters. here also how many parame you can have two three there is no limit for how many number of parameters according to your function requirements you can have any number of parameters created here okay but when I call this function add okay if I directly call like this a will be storing undefined b will be storing undefined and undefined plus undefined
16:59 is called undefined and sum is undefined or something will come okay instead of that if you pass a data like you know 1 comma 2 if I pass 1 comma 2 what will happen one will go into a 2 will go into b 1 + 2 is three sum will become three sum is three will be printed. Okay. Now if I say sub 5 pi comma 1 5 comma 1 if I say pi comma 1 sub okay this function will be called and here five will go here and one will go here 5 - 1 is four result will be four okay now let's uh run this code pri js pree dot sorry I just made a mistake pri do js. Okay, you see sum is three and I'm this
17:46 is a function calling statement and it's passing uh there are two arguments here. Okay, here two arguments are there. Two arguments. Why I'm passing two arguments? Because here two parameters are there. A and B are the parameters. Two parameters are there. Okay. In the function declaration there are two parameters separated by comma symbol. You see first parameter is separated by comma symbol. If you have multiple parameters you have to separate each and every parameter with a comma symbol. Okay. Here also two parameters are there. And here also since there are two parameters if you pass two arguments they will be passed to the respective parameters. Here also two arguments that is two data that is passed. So five will go into the x and one will go and four will be printed using subtraction is four is something you will get in the result. Okay. So this is how we can create functions in JavaScript guys. This is how we can create functions uh okay in JavaScript. And uh the next thing the final thing that I will be
18:33 covering as part of you see I don't have to cover too deep about this functions topic. I don't have to cover the things in very depth. Okay, whatever I'm covering will be more than enough. A lot of advanced or additional topics will be there. But don't worry about when the right time comes, I'll explain all the things about other things. But for now, what exactly is a function? How to call the function? Uh function can have a single parameter. Function can have more than one parameter or any number of parameters. Now the last topic for this session is okay here till now till now guys the functions are okay. In the first example when I have taken functions are not intaking any data neither in the function calling statement you are not passing any data or this particular function is not in taking any data. But if you go to the function two this particular function is intaking one single data it's intaking. But is this function returning anything
19:20 back to the function calling statement after running the code inside the function. Is this particular function returning anything back to the function calling statement which has called and because of which it got executed? No. Right? So this function is not returning anything back to the function calling statement. Only the function calling statement is passing this argument to this parameter. But in return, function is not returning anything to this function calling statement till now. Right? In whatever the examples I have taken here, two two parameters are in taking these two arguments in this function. But is this particular function returning anything back to the function calling statement? No. But can functions do that? Can functions return something back to the function calling statements? Yes, it is possible. Guys, I'll just demonstrate this. This is the last last one for functions guys. So here let me create a function guys.
20:06 Function I'll just write add okay a comma b starting of the function ending of the function. Starting of the function ending of the function here I'll say let sum is equal to okay let sum is equal to a + b. Okay now this is a function function name parameters. Okay. If I write like this, okay, for example, if I write like this sum, okay, when I call this function, when I call this function like add of 3, 4, if I call, what will happen? Okay, this is a function calling statement. It will call this function and the block of the code which is there inside the function will be called and three will go into a and four will go into B and 3 + 4 will be seven, seven will go into the sum and when you write
20:51 console.log here, seven will be printed here. Okay. But is this function returning anything back to the function calling statement? Remember a situation where you have attended an event or birthday party or some kind of event conducted by your friends and family members. Okay. Whenever you go to their function, right? What you will do? You'll try to give a gift, right? Okay. Let's say this is you who is attending this particular function. Okay. This is this is you. You are invited as a guest to this particular function connected by your friends or some circles. Okay. While going to the function, right, you have taken this particular gift. You want to present these gifts. Three and four are the gifts. That gifts are taken by the people who are conducting the who are ar who are organizing that function or who are celebrating that function. Okay. The people who are celebrating that function are in taking your gifts. Okay. Whatever the gifts you brought,
21:37 right? They have collected it. A is taking three and B is taking four and all those stuff. 3 + 4 is seven. All those stuff. And here seven got printed. But the uh you see sometimes in in India and you know most of the cases right in tradition in Indian tradition and all what generally happens is whenever you attend a function with some gifts they will take the gifts and also in return they'll give some gifts known as return gifts. Okay, have you heard about this concept when you attend some functions in India? Okay, the people who are conduh you are celebrating the function uh to celebrate their happiness with you and as a token of appreciation or happiness that you have attended their function they want to give some written item okay as a gift okay since you attended my function I want to give you some gift okay as a memory of that or as
22:25 celebration of or happiness in which you have attended my function okay that is one of the norm that people are doing but here you attended this function as Say as a guest there is a function calling statement as a guest has attended this function and has taken these gifts. The gifts are utilized by this people who are celebrating this function and uh they are not returning anything back. They are not giving any return gift back. Okay. But what if I say they give some return gift. Okay. So here if I run this code first of all if I run this code you see seven is getting printed. Okay. The sum is getting printed. Instead of that instead of printing here what I can do is I can make this function return something back to the function calling statement. For that I have to use a keyword known as return keyword. Remember guys remember
23:11 in one of the previous session where I was explaining about the JavaScript basics and in the JavaScript basics in the case of flow control statements. In the flow control statements the last statement is transfer statements break continue were the two items I already have demonstrated as part of this session in the previous sessions. Right? Along with that along with that I also covered okay uh along with that I also mentioned that there are two more important transfer statements in JavaScript apart from break and continue there are written and throw okay two more are there I told you that I'll explain about the written keyword here written transfer statement here in JavaScript when the time comes now when I'm explaining about the functions the time has come so written is one of the transfer statement okay transfer flow control statement in JavaScript guys okay transfer flow control statement in JavaScript which I'm using here return
23:57 is a keyword which is a one of the transfer javascript statement after return I'll simply say sum okay that means whatever the result of this a plus b that is sum okay that is seven right that that seven will be returned as a result okay as written by written by the function to the function calling statement okay now if I run this code what will be the output will the seven will be printed here three will go into a four will go into b when I call this function and 3 + 4 is Seven seven will be stored into sum and this particular function is returning seven and that seven value is returned back to the function calling statement. But is this function calling statement doing anything with that return value seven here is anything any any operation this particular function calling statement is doing on the returned value that is
24:43 seven by this function function to this return calling statement no right I'll simply say node okay run this code you'll see no output will come okay you see no output the reason behind that is this function is actually returning the sum of these two two two variables back to the function calling statement But this function calling statement is not utilizing that. How to utilize that? Here I have to say let okay let result is equal to or r is equal to whatever you want right okay whatever the value that is written by this function function with the help of the return statement will be written to this function and that will be assigned to the r and if I write r here if I write if I print r here what will be
25:29 printed? Okay. 3 + 4 here. 3 + 4 7. 7 7 will be written to the function calling statement. This seven will come here. Seven will be stored into R. And when you print R, seven will be printed guys. Okay. You see seven got printed. Other way is that if instead of doing like this guys, instead of doing like this, okay, when you're you can directly do like this console.log directly call the function inside the print statement itself that is 3, 4 like that you see. Okay. Instead of calling this function and storing that result into the variable and then writing a separate print statement for printing r right instead of that console.log directly call the function here itself. What will happen when you call this function in the print statement add of 3, 4, 3 will go into a and four will go into b and 3 + 4 will become seven. Seven will be stored into sum and this function will return the seven. Back to the function
26:15 calling statement. Where is the function calling statement? This is the thing. Okay. Here seven will come. Seven will be written by the function. And since console.log of seven means seven will be printed. Just see here seven will be printed. So functions can return the data also only one data it can return. You can have any number of parameters but only one one value it can return back to the function calling statement. So there are functions which doesn't have any parameters. There are functions which can have a parameter. There are functions which can have multiple more than one parameters limit unless there's no particular limit. And also when you call a function a function can return the data back to the function calling statement also. Okay. Using the return keyword. Okay. So this result of this a plus b will be returned back to the function calling statement and that you can store into the variable or directly print them in the console console.log. Okay. And many other advanced concepts
27:02 of functions are there guys. But at this point of time I'm not going to cover them. I'll definitely cover them to you in the upcoming sessions. Okay. So guys uh that's all for this session. Okay. Hope you have understood what exactly are functions and how to use them in JavaScript language. Okay. So in the next session I'm going to cover about the local and global variables in JavaScript. Till then see you. Bye-bye.