Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then.

          So when we've been creating functions so far, all we've been doing inside the function is logging something
          
          to the console.
          
          And that's fine, but we might not always want to do this.
          
          We might want to instead have the function return a value to us.
          
          For example, we could have a list of radiuses or radii.
          
          I think the plural is called, which we get from either a user or a database or something else, and
          
          we might want to pass each of those radii into a function that calculates the area of a circle.
          
          Now, we don't want to log the area of the circle each time we call that function.
          
          Instead, what we want to do is receive the area value back so we can do something with it later on.
          
          So let's create this function.
          
          First of all, I'm going to comment this old one out and down here I'm going to create a new constant
          
          called calc area and set this equal to a function.
          
          And this function is going to take in a parameter which I'll call radius.
          
          And inside the function we want to calculate the area of a circle.
          
          So we'll do that.
          
          We'll say let area equal to pi, which is 3.14 times the radius squared, so to the power of two.
          
          So now we have that area value, but we don't just want to log it to the console, we don't really want
          
          to say console, dot log area because if we do that, then if we call the function later on calc area
          
          and pass in some kind of radius, then it's going to log this to the console.
          
          But then we can't do anything with that area later on down the file because it's not stored in memory
          
          anywhere.
          
          It's only stored locally inside the function.
          
          So we see the area right here, but if we try to use the area later on, then it's not going to work.
          
          I can't say console, dot log area, that's not going to work because this is a local variable now inside
          
          the function only.
          
          So instead it would be nice to return this area value so we can do that by using the return keyword.
          
          And then the thing that we want to return.
          
          In our case, we want to return this variable area.
          
          So now when we call this, we're not logging the area to the console anymore.
          
          Instead we're returning a value.
          
          Now what's going to happen if I save this and come over here?
          
          We don't see anything.
          
          So what we need to do is take the value that is returned to us and do something with it.
          
          Now, when a function returns a value, what we need to do is store that value in some kind of variable.
          
          So what I'm going to do is say const area is equal to calc area and then pass in five.
          
          So what this does is call a function.
          
          It returns the area and the area now is returned here.
          
          So it's set equal to this new constant which has global scope area.
          
          So I could say now console dot log area, oops area and that's going to work.
          
          So if we save this and view, we can see it.
          
          And these two things don't have to be called the same, by the way.
          
          They just happen to be called the same.
          
          So if I want to call this a and console dot log A, that would still work.
          
          Okay.
          
          We're just storing the value that we get back regardless of what it's called here inside this new constant.
          
          Okay.
          
          I'm going to change that back to area because it makes more sense to me.
          
          So that's how we return a value.
          
          Now, just a little bit of cleanup here.
          
          What I want to do is get rid of the return here and just place it directly here so we don't need to
          
          store this in a local variable.
          
          First of all, we can just return it directly that's going to calculate this value and return that value
          
          directly.
          
          So that's just a bit of cleanup.
          
          So this is still going to work refresh and we still get 78.5.
          
          So the benefit of this is that we can now take this area and do something with it.
          
          We could create another function to calculate the volume and pass in the area.
          
          For example, we could say const Calc vol is equal to a function which takes in an area and inside that
          
          we'll do something to calculate the volume.
          
          Now we could call this later on by saying Calc vol and pass in the area right here.
          
          Okay.
          
          So we're able to reuse a value that a function brings back to us.
          
          And I forgot my semicolon there.
          
          I'm going to save that.
          
          So there we go, my friends.
          
          That is how we can return values from functions.