‫So in the last video, we saw how to create a very basic function.
‫And we also saw that a value can be returned from a function.
‫In this lesson we'll look at how to accept parameters or arguments.
‫And I'll talk about the difference between the two.
‫So parameters can change the way a function works.
‫We pass in custom values.
‫So to give you an example a very simple example, let's create a function called add that adds two numbers
‫together.
‫So in order to do that we want to take in.
‫We want to put two parameters here.
‫Let's just call them a and b.
‫So they're just variables.
‫So we do need our number sign sorry not number sign our dollar sign.
‫And we can then use those values in this function.
‫And what I want to do is return whatever A is whatever a plus B is okay as simple as that.
‫And then down here, since we returned it, we and if we want to print it on the screen, we do have
‫to echo it.
‫We could put the result in a variable and use that.
‫But let's just echo out add.
‫And let's say one and two.
‫So if I save that and I run it, we get three and I'll make this a little bigger just so you can see
‫it.
‫And I could call this as many times as I want.
‫Let's say we want to pass in 100 and I don't know, 250.
‫And let me just echo out a line break here.
‫Okay.
‫Reload and we get 350.
‫So that's how we can create parameters.
‫Now a lot of people get confused between the words parameter and argument, and they're often used interchangeably,
‫which is fine.
‫I use them interchangeably.
‫But there is a difference.
‫So these are parameters, the variables that you're adding here when you're defining the function.
‫Those are the parameters.
‫The arguments are the actual values that you're passing in.
‫In this case one and two.
‫In this case 102 50.
‫These are the arguments we're passing in as parameters to the function.
‫So that's the difference.
‫But again you can use them interchangeably.
‫So you can have default values like I just want to show you what happens if.
‫Let me just put another line break in here.
‫So if I echo out Add and I don't pass anything in then I'm going to get an error.
‫It says too few arguments because it's expecting A and B, but I'm not passing anything in.
‫So if you want to be able to call it without passing arguments in, you can set default values simply
‫by setting an equal sign here.
‫And let's say we want a to equal one and b to equal one if nothing is passed in.
‫So that way if I run it with nothing passed in, I get two because one is the default for both of those.
‫Okay, so pretty simple stuff.
‫And just to give you another example from the last video, when we had the say hello, let's say that
‫we want this to now take in a name and we want it to let's return get in the habit of returning, because
‫very rarely will you just echo out something from a function.
‫So let's return the string of hello space and then we'll concatenate on whatever the name is.
‫And then if I say hello and pass in here John then.
‫Up.
‫We need to echo it out.
‫Of course, if we want to print it, it is in the function.
‫It's returned and it's here.
‫But I wasn't I wasn't outputting it.
‫So now I'm going to echo it.
‫And there we go.
‫Hello, John.
‫And again if I don't pass anything in here I'll get an error.
‫But I can put a default value here of world.
‫So now if we reload and we don't pass anything in then it's just going to say hello world.
‫Okay.
‫Now the last thing I want to I want to show you in this video is that we can have variable arguments,
‫meaning we might not know the the amount or the number of, of parameters or arguments that we want
‫to pass in.
‫So in that case we can use something called the splat operator which is just three dots.
‫If you're coming from the JavaScript world, it's called the rest operator.
‫And in other contexts it's called the spread operator.
‫So let's create a function here called add all.
‫And let's say we want to be able to take in as many numbers as we want, as whoever is using this function
‫wants, and add them all together.
‫So we could use the splat operator, followed by just whatever variable name you want to give it.
‫In this case, we'll use numbers.
‫You might often see something like args or arguments, something like that, but I'll be more descriptive
‫and say numbers.
‫And then what I can do in here to add them all together, we can say total and we'll set that to zero
‫by default and then loop through the numbers.
‫I'll use a for each.
‫So I'll say numbers as.
‫Number and then I want to just keep on adding them.
‫So I'll take that total that starts at zero and then add on to that with plus equals whatever that number
‫is.
‫So remember the plus equal.
‫We went over that a little while ago.
‫This is the same as saying total equals total plus number.
‫So it'll just keep on adding them.
‫All right.
‫And then finally after the for each we want to just return whatever that total is.
‫So now if I were to call add all.
‫I could pass in as many numbers as I want and it should add them together.
‫Let me just grab a line break here.
‫And reload.
‫And again, I have to echo that.
‫And we get 15.
‫So it's adding these all together and I can put as many as I want in here.
‫So I can add more numbers.
‫And again it'll just loop through them, add them together and return the total.
‫So hopefully that kind of helps you understand parameters slash arguments in the next video.
‫I want to talk a little bit about scope.