Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          All right then.

          So sometimes we like to pass values into functions to do something with that value.
          
          For example, we could want to pass in some kind of name to the speak function up here and then maybe
          
          output that name in the console.
          
          So to do this, we first declare that we want to receive some kind of value inside the function, and
          
          we do that inside the function parentheses.
          
          So we name this something and I'm going to name it name because that's what we want to receive.
          
          Now what this does is create a local variable called name that we can use inside this code block.
          
          So if I wanted to, I could now output that inside here.
          
          So I'm going to convert this into a template string which says good day and then outputs the name.
          
          So if I try this at the minute when we call it, we're not actually passing a value into the function.
          
          So this is not going to work.
          
          So if I save it and preview it, we can say good day undefined, which makes no sense.
          
          So what we have to do is actually pass a value into the function.
          
          So say for example, I come down here and say Mario and save it.
          
          What's going to happen now is we're going to call this speak function, but we're going to pass in this
          
          value as well.
          
          And then when the function is called, it takes that value and it assigns it to this name variable that
          
          we created.
          
          And now we can access that value inside the function.
          
          So if I save it now and preview, we can see that now it says Good day Mario, because it's taken this
          
          value and assigned it to this local variable.
          
          Now we can't access the name outside of the function itself because it's a local variable, so that
          
          doesn't work.
          
          So when we're trying to do this, nothing's going to happen.
          
          This variable has the scope of this function and can only be used inside it.
          
          So then when we pass in a value like this to a function, this right here, this is known as an argument.
          
          When we receive that value up here and we create these different values inside the function, these
          
          are called function parameters.
          
          Now, the two names are interchangeable and a lot of people get them mixed up, including me.
          
          So you probably sometimes hear me say this is an argument or this is a parameter and vice versa.
          
          But the names are just names.
          
          Most important is this idea of passing a value into a function which we can then use.
          
          Now, sometimes we might want to pass a few different values into a function so we can do that by,
          
          first of all, declaring the different parameters in here, comma separated.
          
          So I'm going to take one in called time as well.
          
          And then same again down here, comma, separate the arguments.
          
          So I'll pass in morning and then we can output that instead of day, we can output the time.
          
          So we'll do that.
          
          So now it should say good morning Mario, because we pass in Mario and morning.
          
          So if I save this and preview we can see.
          
          Good morning, Mario.
          
          Now the order is important.
          
          The order of the arguments must match the order of the parameters.
          
          We can't pass in the name second and the time first and expect that the function is going to know what
          
          those things are.
          
          Know it assigns the first thing here to the first thing here, the second thing here to the second thing
          
          here.
          
          Third, third, fourth, fourth and so forth.
          
          Okay, so then what if we do something like this?
          
          Just call speak and we don't pass in any values?
          
          Then we've seen this.
          
          We get good, undefined, undefined, and that doesn't make sense.
          
          So to combat this, just in case a function is invoked without passing anything in, what we could do
          
          is give these parameters some default values.
          
          So I could say something like, this name is equal to Luigi and the time is going to be equal to night.
          
          Now then if we invoke the speak function, but we don't pass any arguments into the function, what's
          
          going to happen is the function itself will assign these default values to these parameters so it will
          
          say good night.
          
          Luigi So if I save this preview we can see good night.
          
          Luigi But the moment that I pass in some arguments, for example, I'm going to say Sean and day, the
          
          moment I do that and invoke the function, these values are going to overwrite these values so we can
          
          see.
          
          Good day.
          
          Sean So what happens if I just pass one of them in?
          
          Well, the first one will overwrite this value, but the second one will still be the default value
          
          because we've not passed in a second argument.
          
          So if I save it now and preview, we can see Good night, Sean.
          
          We get Sean, but night is still the default value.
          
          So then that is how we pass in arguments into a function and how we receive those as parameters inside
          
          the function so then we can use them.