To make a function truly valuable to be used with other code, we need to have a way for it to return the results. Once it's done all of its computations, it really doesn't make sense to have a function. And then for each different case, you use it and go into that function and modify the code. So that it does with the results what you want it to for that particular bit of code. The best way to make functions reusable, and to make them separate pieces, is to return values or return data from them. And then we can write additional code once we call the function to do what we need to with that value.
In the previous example, we we had a reason to do that. Here is our previous example, we created a function that returns a random number. Well as a part of the function we're logging it to the console. And so really, we've limited what this function does. Yes, you can use the random number function as long as as long as the only thing you want to do is log that number to the console. Well, let's make a change here.
Let's enter keyword return. Now when we call random number, it's going to return the value. That's what the return keyword does. So we're calling this function from somewhere else in our code. I'm going to go ahead and set the results of calling that function to a variable. Now I can do with that variable, the contents of that variable, the value of that variable, anything I want if I simply want to log into the console, I can do that.
But I could do other things as well, I could use it as a part of something else I was trying to solve that needed a random number. All right, let's go ahead and look at what we've got so far. So as you can see, we're still displaying a random number. It's just we're not doing it as a part of our function, so it makes the function much more usable. We use the return keyword to return the value. Now, let me talk just a little bit more about the return statement.
Whenever a function encounters a return statement, the function stops executing. So I could have additional statements below this return statement. They would never execute, because once return is hit it stops that can be used to our advantage at times. For example, let's say We wanted to check to make sure that somebody passed in a number for minimax. And they weren't under fine. I could use an if statement.
If man is equal to undefined, and then a logical OR max is equal to undefined, so if either one of those are equal to undefined, then what do I do? Well, I may simply want the function to return undefined. If numbers are not provided, like needs to be provided for this function, let's just return undefined. Well, I can do that with the return statement. Return. And since not, it's returning nothing, it will return undefined.
And the rest of these statements will not execute. Let me just change this so we can see that that's the case Jump out to the browser refresh. And what we get back is undefined. So that's another use of the return statement. Now I have to return statements in this function. This return statement is only used if valid arguments.
Were not passed into the function. Otherwise it will return something that is useful. All right, before we leave this concept of return, I want to talk just a little bit about refactoring. refactoring is a term we use in programming when we come back to some code and improve it. Well, now that we know return, we can do a little bit of improving on this and make this code much simpler. For example, this whole function could be done like this math dot floor and inside of that math dot floor, we need to do math dot random.
And we need to multiply that by more parentheses here. One plus Max, minus min. And then we want to make sure we add them in on the end. So all of this inside here will happen before we do math dot floor. order of precedence. The parentheses inside are done first.
So we do one plus max minus min. Then we start from left and go right sweet. Get the random, we multiply it by that, and then we add them in. And then we do math dot floor and all of that. So then this function becomes much simpler and a lot less code typed in. We still keep this if statement.
So if something comes in undefined, we simply return. Let's go ahead and save that and see if things are still working in the same way. Okay, we got our undefined value. Let's go now with our max at our max value to it. Refresh again. 1415 1412.
So we're still getting random numbers, which is great. All right. Alright, let's go ahead and move to the next concept.