Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
These say whether this function expects to have data passed into it, and in this case empty parentheses means no, it doesn't. Now after the function is declared, then we can call it. Sometime in our code we can use the name of the function with the opening and closing parentheses, semicolon--because it is a full statement--and all the code in the function will be executed. We could do it again. We could do it again. Now, note that as soon as you put your code in a function, it won't execute.
I am just saying here I expect to be passed one piece of information and I am going to call it x. So in the code inside myFunction, I will have this variable called x that I can play around with. Or if I want two, three, four, parameters, I just separate them with commas. You call them whatever you want. It's the same rules as variable naming. So defining a function that takes X,Y means that inside the code I can just treat X and Y as if they exist, in this case making a new variable from them.
You don't have to return anything from a function. It's up to you. Now what this format means is that later on I could use this function to create a result. And in fact in the last line here, I am creating a new variable and setting it equal to whatever is returned from passing in the numbers 6 and 9 into my function. Now if a function does return a value and you don't do anything with it, you are just calling the function. That will just be ignored. Now one of the usual issues with functions that take parameters is what happens if you get something wrong with them? What happens if you pass the wrong amount of information in there? Let's say we have a function defined called calculateLoan.
That extra one we drop off the end. We are effectively just running the function just fine. And the flip side of that, what happens if I pass in too few parameters? What will happen is that the missing one will be passed as undefined. interest and name in this case will be undefined variables. Now your function code might be able to deal with that, and it might not. So it doesn't necessarily mean it will work, but this is what will happen. And one last thing, we are going to talk about something called variable scope here.
Now, scope of a variable simply means where is this variable visible? What part of your code can see it, what part of your code can use it? So, I have got a function declared here called simpleFunction. It has got some code in it, and then we have a line that declares a variable foo=500. We then have console.log foo and if we call this variable, it's going to output 500 to the console. But let's say right after I call this variable, I then write another line of code that says console.log(foo), and then I run console.log(foo), trying to write out the contents of that foo variable.
This doesn't apply to other code blocks like loops and if statements and so on. It's a very simple split. You use var inside the function, it only exits inside the function; otherwise, it's available everywhere.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101672 Viewers
61 Video lessons · 88422 Viewers
71 Video lessons · 72266 Viewers
56 Video lessons · 103990 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.