02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Sending Multiple Parameters

How to pass more than one variable to a function.

Video Transcript

00:00Now that we got under our belt working with functions sending them an
00:05Attribute sending to them a property a parameter whatever you want to call it
00:10Let's see how could we add multiple parameters to a function?
00:13How could we for example as we've seen in our math?
00:18Dot what was it we did min right and we could send to it multiple values now
00:24I'm for now going to ignore the fact that our third parameter basically tells us and
00:29Just just for now you just take it as a fact that
00:33When you see the rest rest param in your parameters of your of your function
00:38You know that this function could accept an unlimited amount of parameters
00:42We're not there yet because we still need to learn about objects, and then we're going to revisit that but for now
00:48Let's assume that the min function could accept only three parameters
00:52And let's build our own function
00:54that enables us to see exactly the same type of functionality where
00:59We will send the function three variables, and it would trace out to us the lowest value so to do that
01:06I'm going to build a new function
01:08And I'm going to call it min same as we have in our math class, and I'm going to open a round bracket
01:14and as we added one variable in our previous sample where we just typed the variable name and then
01:21colon and the type of the variable which is a number in our case
01:25Because it could be any number any type it we don't want to define it as a floating or as anything else
01:31We just want it to be a number
01:33The way we add multiple parameters is by just adding a comma
01:38Similarly to how we talked about and we said I'm just going to add the three parameters before I get a little bit deeper into
01:43That oh, we didn't want to know we want to type number
01:47And we want C as a type number as well
01:51And we're just going to close the round bracket open a curly bracket and close a curly bracket
01:57Now I'm saying that this should look a bit familiar
01:59And the reason is if we wanted we could have copied all this information and created our own variable definition and
02:06This is completely valid although. It's not really recommended to work in this way
02:11You could break the lines if you want to it's it's not recommended to work in this way
02:16But you could define your variables in this way the only place where it's
02:21The only way you could define your variables is with commas is in functions
02:27Okay, so I'm just going to delete that out because we don't really need that it was just a quick reminder
02:31And let's see how do we build our function now one thing that we want to do here is add another variable inside of our
02:38Function that's going to help us define. What is the lowest value?
02:42So I'm going to create a new variable, and I'm going to call it underscore min
02:46So we don't get confused with our function name which is min and it's going to be a type of number
02:51Now the first thing I want to do is I'm just going to put the first value in I'm going to put the
02:55value of a into our min fun in our min in our underscore min number
03:00Now after we have a value in our min. Let's ask if min
03:04underscore min is
03:06Larger than B. What would happen if min is larger than B if min is larger than B
03:12That means that min is not the smallest value and thus if min is not the smallest value
03:17Then B must be smaller than our value, so we should put B in our value and
03:22Again, we'd want to do the same type of if statement regarding RC, so we could ask if min
03:28Underscore min is smaller. What is larger?
03:32Right is larger than C. If it is larger than C. What do we want to do? We want to make our min
03:38Equal C now. Let's look at let's look at this logic again and kind of see what's going on here and notice
03:44So to then I'm asking two separate if statements. It's not an if else statement. They're completely disconnected and
03:51the reason is no matter if min is larger than B and might be larger than C as well and
03:57B might be larger than C so basically in our first line
04:00we're assuming that a is the smallest now when we ask our if statement if
04:05B was the smallest now our min equals B if a was the smallest and a is still our min
04:11So now we know that the value inside here is smaller than a and B
04:16And the last part we need to ask is if our value is smaller than C is larger than C if it is larger
04:21Then we want to make our minimum to be C
04:24So at this point after we completed these two if statements
04:27We know for sure that min is our smallest value so we could trace out the value of min
04:33And if we run our application again, well, we have to first send to our function our min, right?
04:41So I'm going to send the same values. So I'm just going to use my old technique of a command comma
04:46And I'm just going to copy all the values in there paste it inside of our min which accepts the same three
04:52parameters and let's see if we're going to get the same that if we will get the same value back and
04:57We see that we're getting the same value back
05:00So that now you see how you could work with multiple parameters and there's no limit you could put one you could put two you could
05:05Put a hundred if you want to now, obviously you don't want to overdo it
05:09You don't want to put too many parameters because then it's going to start getting confusing
05:12A good rule of thumb would probably be try to avoid adding more than five parameters the less the better
05:19Most of the times although some languages
05:22Try to put as much as possible because they think it's more descriptive
05:26It's more convention flash to kind of keep it to the minimum bare minimum that's needed
05:31Okay, so the last thing that I really wanted to talk about this is just a reminder again
05:36We're missing one thing in our min function. What are we missing?
05:39Our min function isn't really returning to us the value
05:42We can't put it into a variable and then work with our minimum value because it's just outputting it into the screen
05:48Although we have that value in our min inside of our function. We don't we don't have a way to retrieve it

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!