Learn Javascript Programming #6: Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's up guys this is Tenzin with whatsapp calm and today we're doing functions JavaScript functions and if you're wondering what I have been doing the past two and a half months long story short I've been living under a rock and I've been really really really really lazy and I haven't produced a video in the last two months that hurts I apologize guys I will starting today there will be a video every single day let me repeat there will be a video every single day okay yeah let's let's get on with the video yeah shall we yeah yay okay so functions are super cool right but before we talk about functions I want to talk about variables a little bit first okay so why do we use variables right we use variables begin we because we can use them again and again right so for example if I had this VAR name equals not name let's say fruits or people right and let's say we have like thousand people in this array like thousand people right I'm not going to write thousand people but let's say Joe John okay now we don't have to keep writing this array every single time right if we ever need to reference to this refer to this array we just do console.log people right or for for loop right people person of people on and then console dot log person okay and that work and if I just remove that it'll just do both right Joe John and if you have more it will print out a little console down log all of them right and that's why variables are useful because we don't have to remember what is inside that's right in the same way functions operates in a similar fashion so variables store static information right array string numbers whatever but functions store specific tasks think of variables as nouns and functions as verbs okay so for example if I write a function right now so this is how you write a function function the keyword function and then the function name let's write a function that greets cats okay cat greeter okay and then you write the parens two parens open closing and you write two Curly's opening and closing and all your function logic goes in these within these curly braces okay so now I can do console dot log hey cat you are a fine animal okay and then I can do console dot log yeah there it is okay cool so now what I've done is I have written a function or defined excuse me I have defined a function okay but I haven't ran it yet so this is called function definition okay so let me just write it up there maybe yeah function definition okay I'm define a function wrote a function whatever whatever makes sense okay and then here I'm going to run the function or call the function or invoke the function or execute the function okay they're the same thing people use different words same thing so I can so to run this function what you do is cat greeter and then current that's it run kay cat you're a fine animal meow right so if I and I can run that again and again and again without having to write console down lock those console dot log statements again and again and these this is just two lines but what if there are 20 lines in there now it becomes even more cooler right because you just do cat greeter and it just turns out 20 in months so let's take a look at a different example now let's say our function we want it to be more personalized personalized okay so cat greeter no no let's do special cat greeter or a special greeter okay and let's say it takes in a name okay input and functions can take inputs okay like this name and you can call that anything you want X Y whatever I'm just calling it name because it makes the most sense so here what I can do is I can do console.log hey name you have a cool attitude okay let's do that so then to run this function what you do is again if I just run it nothing's going to happen and this is let's get rid of that nothing's going to happen okay because I'm just written a function I haven't ran or called or invoked or executed this function yet okay so what I have to do is I'll call this function so I do special greeter name what I'm gonna do let's do Joe boom hey Joe you have a cool attitude on Tenzin hey Tenzin you have a cool attitude oh thank you I know I get that a lot let's do Rafa Kazi hey Rafa cardi you have a cool you don't have a cool attitude you know so you see it works so I'm just kidding he's a cool dude he's a cool dude he's a very cool dude sometimes on weekends only on Sundays yeah afternoon morning hey see he's an ass in the morning okay cool so functions can also take multiple parameters okay so multiple inputs so here we'll say mmm adder or something okay I can do ABCD again you can call them mango apple banana whatever you want but I'm just going to call it ABCD because I'm too lazy to write more so what you can do is console dot log a plus B plus C plus D okay and now let's run it adder and now you have four inputs four parameters right so you have to pass in four arguments again when you write a function these are called parameters ABCD but when you pass in values here when you're calling the function it's called they're called arguments okay so I can do two two two two and that will give you eight right boom eight by five two three that should be nine if I do for that should be ten and if I change this to like three that should be eleven right so it makes sense another example so let's work with a race now okay so we'll do function printer arrey arrey printer I don't know printer let's just do printer all right whatever and let's say it takes in an array of animals okay and you just loop through the animals and print it out good this function okay so we'll do is we'll take it in the right I can call it anything I want again I can do for bar I equals zero I is less than array dot length okay this AR are again let me just call this mango for now whatever okay mango dot length because we want to do it however many we want to continue the loop however many items are in mango okay it's going to be an array so then I plus plus and I'm just going to console down MOG mango pie okay which likes which goes through each element and print it out so now let's say I have I call this array and call this function and let's pass and fruits what am I supposed to do animals yeah let's do animals cat kangaroo spider and finally raccoon that's nice belly I think that's high spell it I don't care cool cat kangaroo spider raccoon and if you add more let's do Joe Joe there it is right Joe's an animal alright so let's move on to another example and now let's talk about the console dot log statements okay so so far in our functions we have basically used console log statements and we are all familiar with that right but now let's talk about return statements okay and console dot log statements are cool but not so cool okay and I'll explain why in a bit so let's write a function here and let's do adder I think we already have an adder right okay let's just call this adder multiple okay and it's called pattern and let's say it takes in two numbers num1 and num2 okay and return num1 plus num2 okay so basically this adder function takes in two numbers two parameters both numbers and we return the sum of those two whoops one okay so now if I run this function let's see what happens adder two four six right so now you're wondering hey Tenzin um then what's the difference it's doing the same thing console dot log and return are both doing the same thing okay if I do console dot log here while it still returns six but this is though this is a white six and the other one was a green six right but the colors colors don't matter okay it's just the repple that's doing it for you to like to let you know that there are different things but okay the colors don't really matter what's happening here is you use return statements for functions okay and that's because when you use a return statement the function is actually giving you an output okay when you are console logging it's it's like you can console dot log everywhere okay so like here I can do console dot log hello right I know console dot log hello but I can never do return hello randomly like this they'll say return outside of function right because returns go inside a function okay so in programming in computers everything is about input and output right and functions are like the perfect example for that so functions takes an input and that it's supposed to return some output okay and if we do console about log that's not really really outputting anything that's just logging it out to the screen so you can see but it's not internally outputting anything at all so to show you the difference and not just talk what I'm going to do is VAR result let's do ya result equals adder to four okay and you can do that so now if I do console.log results let's see what happens six right because adder to four returns six and then it's sort and result and we can tell about mark result okay that's cool right but now let's see what happens if I change this guide to console dot log ok let's see what it says whoa what happened it says six and undefined okay so what's what's happening is you're running this function and it's console dot logging num1 plus num2 which is cool but right right here in line 39 for a council about logging the result variable and it's saying that's undefined right it's saying I don't know what that is it's just undefined I don't know what it is so to make it clearer I can even do the result is okay so you guys can clearly see that it's undefined and it's actually printing out result here okay and that's because console dot log doesn't I'll put anything ok so again let me change it back to return okay look at that I can even do I like result plus two so now six plus two which is eight right I can do times 78 there it is the result is 468 so essentially what you want is return instead of console dot log inside of functions okay but it's not to say that you can use console dot log in functions you can definitely use console dot log in functions for debugging too for like learning purposes but if you're writing a function 99% of the times you can be doing return statements okay instead of console dot log okay cool so here this is cool because we're just printing printing stuff out here you should what you should have is return itself console dot log so now let's do another example knowing this fact let me just comment that out yep cool cool cool okay so now let's do a function here and let's say it's a problem that takes in two arguments or two parameters right this is called parameters sorry sorry sorry my fault so let's say it takes in two parameters one is the first one is an array okay and I'm just calling it array let's just call it numbers nums and then you have the second one as a number okay now send them okay and now what you want to do is this function should check if none exist in notes so now when you run this like this exist and let's say you have one three five seven and you have two they should return false because two does not exist in this array right but if you have one this should return true okay so now let's see how it works so if i do for VAR i equals 0 I is less than nums dot length right because that's the array i plus plus and now I just do if right if num is equal to okay if at any point if num is equal to num I okay or you can even do nums I okay because nums I is every element during each loop right so if I just counted on log for you nuns I right so we have one three five seven we're just looping through it and just printing it out right but now console dot log is cool but we want is a logic here so if nums i is equal to num right what we're saying is if at any point one of these equals this number whatever it is in this case it's one then I want you to return true and then what you do is you go outside of the loop and do return false so now what you're saying is if at any given Boop the number equals the second parameter this num then return true okay and if the loop is done and it didn't return to it that must mean that the number didn't didn't exist in the array okay and then we do return false and if I run this there is false and if I do too it'll be true and so on okay cool okay guys so that's it for JavaScript functions if you guys think I went a little too fast which I probably did let me know if some parts of not clear I'll happily do functions part two and you know go more in depth about functions and pull things out and the reason why I went a little fast is because initially I had no plan of doing a video today yeah it's really sad but I I've been procrastinating like God and I've always been saying oh I'll make a video tomorrow oh yeah I'll make a better tomorrow yeah tomorrow I have time yeah make a video some more and it never happens it never happens and tomorrow never ends so that I just jumped on my chair and I'm like it's not going to happen this is today I don't care what happens I am going to create a video and it's going to be released today and here it is right so yep that's it for functions and stay tuned and we will talk soon we will talk [Music] Oh [Music]
Info
Channel: whatsdev
Views: 41,819
Rating: undefined out of 5
Keywords: javascript, learn javascript, learn javascript programming, javascript 2017, learn web development, javascript tutorial, html css and javascript, web programming, what are javascript data types, javascript data types and variables, variables in javascript, most popular programing language, best programming language to learn, javascript functions, how do javascript functions work, learn javascript functions, javascript functions tutorial
Id: uiv3oLsHbaI
Channel Id: undefined
Length: 17min 53sec (1073 seconds)
Published: Tue Aug 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.