hey what’s going on guys thud Maya fourth annual earnings and in this video tutorial of JavaScript we are going to be taking a look at one of the most important concepts in GS or JavaScript that is functioning so if you are coming from any other programming language like C++ or Java then functions over your the concept is pretty much the same however the syntax is a little bit different but if you are very new to programming and this is your first programming language then in very simple terms functions are basically blocked of code which is being given a particular name so that we can reuse that block of code again and again
in our entire program so you can see there’s a little bit of theory already functions are one of the fundamental building blocks in JavaScript a function is a set of statements that performs a particular task or calculates a value a JavaScript function is a block of code designed to perform a particular task and a function is executed when something invokes it so you can find
more theory in the video description
I will link an article wherein you can see or everything on functions basically in the theoretical perspective and also the code that we type in today but before we actually get into technicality let me just give you a very basic example so let’s say you want to perform addition of two numbers in your program okay so performing addition what you do is you create two variables where a and you are saying fie you create one more variable rather B equals to 6 then you create another variable where total you see a plus B and let’s say now you want to print this so you see document load right and this is what you do right so if I change the value over here you can see the addition also changes so this is how we perform addition at the very basic level now let’s say in your
program the requirement is that you have to perform the same addition five times so if you didn’t have functions what you
would do is you would just copy this code and probably paste it five more times right so this would definitely increase your line of code and it
doesn’t make sense to copy-paste the same over and over again if it’s going to be doing the same thing so this is where functions come into the picture and basically what it does is what you can do is you can create a separate function and give it a particular name let’s see add two numbers and inside that function, you can add these three lines or these three statements and then you can just
use that one single function name over and over again to call these three lines so you save three lines of code now this
might not sound relevant enough but in real-world scenarios when you’re writing big programs there are a lot of repetitive tasks which are having hundreds of lines of code so you cannot copy/paste hundreds of lines of code in your program over and over again so instead of that you create functions
okay so with this little bit of knowledge on functions let’s try to see what we have in question number one so you can see we have to write a function to add two numbers and print that is it so this is what we did over here but you have to do it using functions so I’m just going to cut this out now the syntax of functions in JavaScript goes as follows you just type in the keyword
a function that’s it so there is no return type required over here because we know JavaScript is weakly typed so we can dynamically change the type of functions and functions are already overloaded in nature and I want to get into a lot of
details of function overloading and all as of now but for now just type in the keyword functions and then you give a name of your function so I’m going to say add two numbers and then opening and closing round brackets which denotes that it is a function and opening and closing curly braces which is the body of the function so there is a naming convention being followed you don’t have
to follow it but generally what happens is the first word in the function name starts with a lowercase then you can
have numbers or you don’t have numbers as your call but the second word usually starts with our buggy’s okay or you can also have an underscore in between you cannot have numbers as starting for the function name so similar to the naming
conventions for variables that same rules apply over here okay so this is the basic structure of our function okay you just write in function and then this is our function name opening and closing round brackets and now
this what we will do is we just cut this coal and paste it over your also we just cut this code and paste it over your as
well okay so we have created our function however you can see in the output we cannot see anything because we still have to call the function, okay so this is just the function Declaration and definition wherein we’ve defined our function you’ve named that function and we’ve told that this is what it is gonna do but in that script tag we still haven’t called this function so there is
a concept of a function called so if I just write down ad number and opening
and closing round brackets and hit a semicolon immediately you can see that we are getting the output that the total is 8 so what we did over here is we call the function so this is a function point so the program knows that, okay so there is a function which looks like this and it goes and checks this function and calls it and all these values are executed so now a function can also have
parameters so what do I mean by parameters so nowhere you can see that we’ve hard-coded these values Phi n 3 but what if you want to pass these values in the function so you can do that also so you can see 8 comma B so these are basically the parameters and you don’t even have to give the datatype because JavaScript is loosely or weakly typed so here what I’m going to do is I’m going to say the total is equal to e plus V and right now you can see in the output we are not getting anything because over here we have not passed anything so since you can see that now our function is a little bit different because it is expecting two parameters over here I’m going to pass two parameters three commas four so immediately you can see we got the output to total is seven now we can also pass strings over here so if I say done my space and over here if I say subfile you can see the total is 10 my space sub
file which means that it just concatenated the two strings so depending upon what datatype you pass in over your a function is automatically adapting or adjusting to the data types because as I mentioned javascript is loosely typed so the data type of the variables is dynamically taken and it can change over the course of time so these were parameter passing in the function so we passed in tanmaya as a value and some as V value so inside the function is holding on my and B is holding some koala and it just took edition than this concatenation stored in the local total variable and just printed it out now lastly what I want to talk about is
written typing functions so the function can also return a value okay right now you can see that our function is doing the printing work so let’s say this statement is over here and the function here is going to just perform the total or perform the addition and return this variable outside so what this return statement does is so whenever you call your function you can see we are calling our function over here ad number so we are passing two parameters than Maya and sample so these parameters go in as a and B we get the total that as we are getting the concatenation of Thun bands and Kyle and then we are getting a return value rate so when this return statement comes in the function this total is actually given out of this function so there has to be a particular
variable to hold this value which is a return from this function so I’m going to say where output equals two and on the RHS I am calling this function so you can see we have equal two in between on the LHS we have a variable on the RHS that is the right-hand side we are calling the function and the reason why we kept it on the RHS is that now our function is returning a value and when it returns a value the assignment will happen from right to left so whatever the function is returning is being assigned into this variable and right now it is returning to my supplies so that’s why it is being stored in the output variable and that is what I am
printing over here in the document dot write function similarly if I just pass in numbers you can see the total is six
so six is being stored and it is returned
using this intern statement so when it is returned it is stored in this output variable outside of the function and then we just print it okay so this was a very quick program and demonstration of functions in JavaScript of course we will go a lot more in detail on functions as we move word in the C’s so this was just a very introductory video on functions of course functions is a very detailed topic and we will get into a lot of more details on functions for example what are group Lupul variables when we use functions when we shouldn’t use functions and what not so we will discuss a lot on functions as we move out in the series this was just a quick demonstration on functions so that’s it for this video guys I hope you like this video if you like this video please give it a thumbs up let me know in the comments how this video was do share it with the friends as well and if you haven’t yet subscribed make sure you subscribe on this channel so that we get notified whenever I upload a new video so thanks for watching see you guys in the next
video peace