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

Function Parameters

At this stage we know how to create functions in JavaScript and we know how to call them. the next step is to know how to send into functions parameters.

Video Transcript

00:00All right, so we've seen a function in the works,
00:03and we've seen probably almost all of the things
00:06that we should know about how to work with functions.
00:09We wanna just talk about another couple of small stuff.
00:11So let's start off with that.
00:12So first of all, let's start creating another function.
00:16And this time around,
00:17let me just comment this function out for a second.
00:21And we're gonna move on to the next one.
00:23And the next one is really gonna be,
00:25we're gonna first see it in action.
00:26So let's call this var nbig,
00:28go back to the demo that I originally wanted to start with.
00:31And this time around, I'm gonna search for,
00:36there's a few variables here,
00:37and I don't remember it by heart,
00:38but let's see if I can find max.
00:39Here we go, there's max and min.
00:41Max will basically accept a few parameters,
00:43and it will return the biggest or smallest value.
00:46So for example, minus two, minus 10.
00:49And if I alert that,
00:51we should get our nbig to be minus two.
00:54And it's that simple.
00:55Basically, I'm sending parameters with a comma
00:57just as we talked about up here.
00:59So if I refresh this for a second,
01:00we're gonna see that our largest number is minus two.
01:03In our scenario with the math class,
01:05we're gonna be able to send more than two parameters,
01:07but we're not gonna deal with that scenario.
01:09We're just gonna show how to build,
01:11for now, in our level of training right now,
01:14how to build a function that could accept
01:15a closed set amount of parameters.
01:18Although the math class,
01:20the math that mass could accept
01:21an unlimited amount of variables,
01:23our function will only be able to accept
01:26an X amount of variables,
01:27and we're gonna define it as two numbers for now.
01:30And we're gonna revisit that math class
01:31as we learn other things that will enable us
01:33to actually create those type of functions.
01:36All right, so let's move on
01:38and actually create another function
01:40which is gonna be similar to the math max,
01:42only not identical,
01:44because this math max,
01:45our math max function, max,
01:49is only gonna be able to accept two parameters,
01:52and it's going to return the largest value.
01:55So we're gonna use an if statement again,
01:58and basically, all we have to do
02:00is create some sort of variable here,
02:02and the most simple syntax here would be
02:04to basically ask if our a is bigger than b,
02:08return a, if our b is smaller than a,
02:11basically just ask, so we're gonna use an if statement.
02:15In our scenario, let's think about it.
02:17If a is larger than b, then return a.
02:22In all other scenarios, we could return b.
02:24All right, so that's gonna be our rule.
02:26So basically variable,
02:27because it could be that they're equal,
02:29and that's fine, we just have to pick one of the two.
02:31So variable, we're gonna call this big and big.
02:36And notice, even though I'm using the same name
02:40as the variable name externally,
02:42which is called nbig, it's totally cool.
02:44It's a completely separate variable,
02:46and why is that?
02:46Because I'm defining a new one.
02:48Now basically, when we define a variable,
02:51we actually say to JavaScript,
02:54what is the scope of that variable?
02:56And basically in our scenario, when I'm creating that var,
02:58usually the way you find out the scope
03:00is by searching for the nearest curly brackets,
03:03opening and closing ones, and if there isn't one,
03:05you're basically creating a global variable.
03:09In our scenario, that's most of the time,
03:12if statements, for example, not true.
03:14So it's only true for when that rule of curly brackets
03:18is when that curly brackets contains basically
03:21a something, and if is not a something,
03:23and if is just a question.
03:24So if it contains a something such as a function,
03:26such as objects which we'll learn about in a future date,
03:30well, not that far away,
03:32is basically going to create a scope.
03:35So anything that has a scope, functions have a scope,
03:39could create internal variables that won't exist
03:42right after that variable function is completed.
03:44So it's kind of like an inner scope of content.
03:48Now if we didn't put your var in,
03:49we just set the value to nbig directly,
03:52what we would do is actually override the value of nbig,
03:56which we really don't want to do that.
03:57That's why we're creating a completely separate variable.
04:01Now, I'm gonna write your nbig too
04:03just so you don't get confused,
04:04but I'm totally allowed to use nbig
04:06because nbig is a completely separate variable.
04:10It's gonna take some time, and this is exactly
04:11the place where a lot of developers
04:13get a lot of errors and issues and mistakes,
04:16is because they don't notice what scope they're in
04:18and they think they're overriding something
04:20when they're not, and vice versa.
04:22All right, so I'm gonna take nbig
04:23and I'm gonna set it to be the value of a to get us started.
04:28And now I'm gonna ask a question.
04:29If a is larger than b, or actually if a is smaller than b,
04:33then we're gonna say nbig,
04:35we're gonna open a curly bracket, close a curly bracket,
04:38and we're gonna say nbig equals b.
04:41Basically saying put inside the value of b into nbig
04:45if a is smaller than b.
04:47And that's all we really had to do,
04:48and now we could do a little return here,
04:51and we could return that nbig value.
04:55So by the way, I made a mistake here,
04:57which JavaScript wouldn't tell me about it,
05:00but I, by mistake, wrote to the earlier name,
05:03and that's because I did this change
05:04and I forgot about it.
05:07So if you make a mistake like that,
05:08you might have this weird bug
05:09that you just can't figure out how to fix,
05:11and it's actually a perfect example
05:13of creating a bug that is almost impossible to find.
05:16All right, so the next step is I wanna actually use that.
05:19So instead of using my math max,
05:21we're gonna come here
05:22and we're gonna call our max function,
05:24and we're gonna send into our max function two variables.
05:27Let's use that na and nv.
05:31And notice, again, you have to make sure
05:33that your variables are the exact same name.
05:36If you use anything like uppercase, lowercase,
05:39all has to match up, and if it doesn't,
05:41then you're just gonna get really weird errors.
05:44And the last but not least, we wanna trace out,
05:46we wanna alert that value to see
05:48if we're getting the value that we would hope to give back.
05:51So our biggest value here should be nv, which is 32.
05:57So if we refresh it again,
05:58we're gonna see we're getting 32,
06:00and we are extremely, extremely happy.
06:06All right, so there's actually a lot more
06:07that we have to talk about functions.
06:10We just really scratched the surface of it,
06:13but I think this is really enough
06:14to start playing with code and being,
06:16and really our goal with this getting started
06:18with JavaScript is to push you as quickly as possible
06:20to start actually interacting
06:22and creating cool things inside of JavaScript.
06:24So we're gonna take a pause on talking about functions,
06:27but we have a lot of videos about functions in general
06:30in our site, everythingaffiliate.com,
06:32which I encourage you to check out,
06:34which will help you understand a much more in-depth
06:38in and out of what functions could do
06:39and how to work with functions.
06:41But we're gonna take a little pause on functions,
06:44and we'll probably revisit this in the future.

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!