Transcript from the "Functions, Parameters, & Arguments" Lesson
[00:00:58] We have the keyword function, which indicates that this is gonna be a function. A name for the function, in this case, half, and then some parentheses with some stuff inside them. Then we have our good friends, the curly braces. And then we have some code inside of those curly braces.
[00:01:19] And in this case, we have one line of code and it has the word return in it. And then it has some kind of expression, in this case, dividing x by 2, so we might expect half to give us the half of some number. And if we wanna use it or as we say in computer lingo, as we if we wanna call a function, we can say the name of the function, and then parentheses.
[00:01:50] And then pass in a value that we want to give to this function for it to run through its code as whatever value we put inside of the parens when we declared the function. So in this case, if we assign a new variable one to the value of calling half on 2, the function half is gonna get the number 2 and it's going to return 2 divided by 2 which is 1, cool?
[00:02:30] Okay, so let's talk a little bit more detail about what is going on here inside of these parens when we declare the function. So the stuff that we put in here, which right now has very boring names of x or x and y, these are what we could think of as the values that this function is gonna need to work.
[00:02:55] And then when we call it, we're gonna pass in actual values that we want this function to work with. And so the difference between these words is essentially that when we're declaring the function and we're saying okay, this function is gonna need an x and it's gonna need a y, we call those the parameters of the function.
[00:03:19] And then when we pass in actual values like 2 and 3, those are called the arguments passed to the function, which are gonna get assigned to the parameters that we described when we created the function. So you could think of these parameters or what happens in this x, y here as sort of like mini-variables or sort of pseudo-variables are what we are gonna call the values that get passed in as arguments to the function.
[00:04:16] Does anybody remember what Math.random does?
>> Random number.
>> A random number, specifically?
>> Zero, one.
>> Between zero and one, yes, exactly. So right now, we have a getRandomNumber function which is really just kind of a tiny little wrapper around our Math.random method, which is going to give us a new number.
[00:04:38] And just like Math.random, we don't need to give this function any values to do its job, it's just gonna generate a new number for us. So in this case, our function has no parameters. In between these parentheses when we declare the function, there's nothing. And when we call the function, we put nothing between the parentheses.
[00:04:59] So yes, so these parameters are the inputs that our function expects, and the arguments that we pass in are the actual values. So let's take a look at this example. I have a function here add3, which is going to take in three different parameters with the very boring names x, y, and z.
[00:05:18] And then it is going to log two things, it's going to log the names of the parameters, and it's going to log the values of the actual arguments. Note the difference in syntax here between these two calls to console.log. So fun fact about console.log is you can call console.log and pass in multiple arguments to console.log using commas to separate them, and it will log them all separated by spaces.
[00:06:43] And then the value of sum gonna be what? What do y'all think? [LAUGH]
>> Indeed, so what add3 did was a few different things. It logged a couple of things to the console and it returned a value, which then I was able to capture by assigning it to a variable.
[00:07:12] So we name parameters similarly to how we name variables. So we should kinda follow the same rules of thumb as we do when we're naming variables. What do you think happens if I do the first thing here? This function, does this work where in between the parentheses when I'm declaring this function, I'm putting a literal string value, literally a value?
[00:07:38] Should we find out what happens?
>> It should work, no?
>> I think you should be able to pass a string as a parameter into a function.
>> So we have some people saying I think so, I think it'll work. Some people saying I don't think it'll work.
>> In chat, true.
>> Let's find out, sorry?
>> In chat, somebody said true.
[00:08:44] Do you think this will work?
>> Yeah? So we talked briefly about variable names earlier in the course, is this a valid variable name?
[00:09:05] It's cuz it can't even parse this as a name for a parameter. It's actually seeing the 1 and thinking it's a literal 1, and then it's confused about what happens afterwards, and so no, this doesn't work. So essentially, when you're declaring a function, put valid variable names as the names of any parameters that you might need for that function, cool?
[00:09:24] Okay, now, another question we might have is like for example, add3, as its name indicates, expects three values coming in because it's trying to add x to y to z. So what happens if we don't call a function with its intended arguments? What happens if we call add3 with just 1 and 2, for example?
[00:09:51] Any guesses?
>> Node reference.
[00:10:10] Any other guesses what might happen?
>> In chat, a couple of people are saying an error.
>> An error might happen, yes, so that's basically more generally. Let's find out. [LAUGH] Well, we didn't get an error. The function ran and it said, I received the arguments 1 2, and our good friend undefined.
>> Not a number.
[00:11:48] Now what about our getRandomNumber function, which, let me just go back and grab it so that we have it. So we had this getRandomNumber function, which returns our Math.random and if we call it, we expect some kind of number between 0 and 1. But what happens if we call it with some value that we didn't account for in the function declaration?
>> It sort of turns optimus quick.
[00:13:42] So, yes, it's sort of almost like you pass in no arguments and it's just like, well, I have no arguments, to you not having any arguments. I don't mind, it's fine, it's loosey-goosey.