Transcript from the "Function Return Values" Lesson
[00:00:23] Make that value the output of this function, the thing that the function gives back. And so that means that we can capture that value by, for example, as we've seen a couple of times, assigning it to a variable. So in this example, our function square it takes one parameter, named x, and it returns the value of the expression that we get when we use the star operator on x and x.
[00:00:52] So we expect square of 3 to be 3 times 3, which is 9. Yeah, question.
>> Would it be different than l console.log, x times x would it be the same result there?
>> Great question, let us find out. So I think the question is if we have this function square, and let's use our original example square here, which says return x times x.
[00:01:18] And then we do const 9 = square(3), and we asked for 9, we have the number 9, yeah? Okay, now, Let's find out what happens if we make a new function. I'm gonna call it, confusingly because of math terms, log square, which is going to instead of returning, it's going to console.log x times x.
[00:01:52] Is this what you meant? Okay, so now if I call logSquare in the console on 3, I'm gonna see the value 9 print out. But what happens if I do const logNine, let's say, is logSquare(3). What happened essentially in this case is a good question. So, let us see what happens when we have a function like this, a function that doesn't return anything, of which our logSquare is another example.
[00:02:27] So, here we have a similar one, it's called, sayHello, it takes in a name and it logs out, Hi, plus name, plus exclamation point. So if I say, sayHello(Marc), I'm going to expect to hear, Hi, Marc. Anybody see The Room? No, it's movie reference, whatever. Now, the question is, what is gonna happen to that value?
[00:02:50] So let's find out in our logSquare example, very similar function. All right, so we printed out 9 to the console as expected. What is the value of log 9 gonna be, do you think? Any guesses? Let's find out.
>> Log 9 is undefined.
>> Because we never passed in the parameter.
>> Because we never, sorry?
>> Because we never pass in the parameter for logSquare.
[00:04:43] So the value of logNine is whatever the value that the logSquare(3) evaluates to, which is the return value of the function, which in this case is undefined. Now what we could do is, let's say have a logAndReturnSquare function, where we first log the square, And then return the square, for example.
[00:05:16] So if we now say, result is logAndReturnSquare, what do we expect is gonna happen?
>> Any guesses?
>> It's gonna be 9.
>> Go ahead.
>> Our result's gonna be 9.
>> So result is gonna be 9, and is anything else gonna happen?
>> Print 9.
[00:05:56] But if I ask for result, it did indeed capture the return value of 9. Okay, so if you want your function to give you a value that you can use later, you wanna a return statement. That doesn't mean that there aren't useful functions. And in fact, we're gonna do some that don't have a return statement, because sometimes we actually want the function to do other stuff then give us back a value.
[00:06:23] Maybe we wanna log things to the console, maybe we wanna change something on the HTML DOM, right? So there are plenty of cases where you're gonna have functions without return statements, and anytime that you don't have a return statement, the implicit return value is undefined. I have one more question for you.
[00:06:43] What if I had a function returnAndLogSquare, which looks similar to our earlier one but with the lines reversed? So here we're gonna have a return x times x and then console.log x times x. Yeah, okay, what happens if I try to create that function, the browser's already giving me a warning that there is unreachable code.
[00:07:14] And if we were to try to capture returnAndLogSquare(3) as a value called attempt. Notice what didn't happen when I called that function, returnAndLogSquare, we didn't see our 9 print out, Like we did before. Now, in this case, we're seeing a 9 because that is the value returned, which was the value that we got in attempt.
[00:08:31] Yeah, question.
>> We have people in the chat saying that the return statement exits the function.