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

Creating Functions

After working with functions the next logical step is to understand how we can create our own functions so we can create our own actions.

Video Transcript

00:00And I want to actually create that math abs function. So to do that, I'm going to just
00:04comment out lines 12 and 13 for a second here. And we're going to create a new function.
00:09So the way to create a new function is really, really simple. There's a few steps involved,
00:14they just have to get used to them. The first is we declare that we're going to create now a
00:18function to do that we type function. Now, all lowercase, the next step is we want to give a
00:24name for our function. In our scenario, I'm going to call our function abs. Now, I'm not
00:30worried about that green color, because I know that that abs is not a reserved word, it was just,
00:34it's just green, because it's part of that math package. But because we're creating this function
00:39globally, it's it's not competing, it's a global function, it's a function that we just have to
00:44type abs, we don't have to type math that abs, so it's a completely separate function.
00:49The next step is to open a round bracket, and then close a round bracket, we could then
00:55open a curly bracket, we're not good, we should, and then we will close a curly bracket. Now,
01:00this is an important part. The here, the first part of the round brackets is really our its
01:06main usage is because with it, we'll be able to send different parameters into the function.
01:11And parameters are variables that we could use inside of the function. The opening and closing
01:16brackets, the round brackets basically define where does the function start and where does the
01:21function end. So I want to create just quickly, just a very, very basic function here. And we're
01:27going to expand it and grow it and make it actually do what we want. So I'm going to put here just an
01:31alert alert. Hello, open inside of a string. And we could save that. And again, if we run our live
01:44code, we're going to see something interesting, actually, nothing is going to happen. So if I
01:47run my live code, we're going to see we're not really getting anything. Now, the reason why we're
01:51not getting anything is very simple. The reason why we're not getting anything is because we're
01:56never actually calling the abs function, we've just created a function, which is actually really
02:00cool. It's the first function we create on our own. But we haven't actually said, I want to call
02:06this function right now. To actually call the function, all I have to do is type abs, open and
02:11close round bracket, the same exact exact way as we called functions previously, and then use a
02:17semicolon. Now, by doing that, if I run my application again, whoops, if I run application
02:24again, we're going to see Hello, which is exactly what we hoped to get. Alright, so just a couple
02:30of small things to note. Now we could call this function as many times as we want. So if
02:36I call it three times, and I run my app again, we're going to get Hello, Hello, Hello, three
02:41times. Perfect. So that worked for us well. Now, obviously, this abs function is really not
02:46complete. It's not really doing what we want it to do. What we need to do next is actually be able
02:51to send into it some sort of parameter to be able to send into it a value. So we'll be able to
02:56actually trigger our step. So first, step one, we've created the function. That's great. Let's
03:03move on to step two. And step two, what we want to do is we want to take that function again.
03:08But this time around, we want to send some sort of parameter into it.
03:12To send a parameter into a function is very, very simple. The way we do it is this inside of our
03:16round bracket, all we have to do is put your variable now this variable is a unique variable.
03:22So I'm just going to call it a, because I want to make sure that you understand that whatever we
03:26send into that very into the function is becomes its own variable. So in our scenario,
03:32I send your a and let me just output a as soon as we get it. Now, to trigger it is very simple,
03:39we're going to call our abs again. And let's say we send inside of our abs 10. With our abs
03:44function, we'll do is basically accept that 10, we're going to call it a as soon as we're in
03:49the function, and then it's going to split out that alert message and output 10. So if we
03:55refresh our screen, again, we're going to see that we're getting 10. And that's exactly what
03:58we hope to get. Now, the next step after this is obviously we're going to try to massage the data,
04:04but I just want you to be sure that it doesn't really matter if I send your na as well. If I
04:09send na, then it's just going to output the value of na. I'm going to save that refresh, and we're
04:15going to see that we're getting that minus 20. So I'm going to click OK there. All right,
04:20so our next step is our next step is to actually start massaging this data and trying to figure
04:26out is it positive or is it negative. To do that, I'm going to use an if statement. We haven't
04:31really learned about them, but they're relatively intuitive. And we're going to talk about them
04:36very, very soon, more in depth. So let's see how it works. Basically, we ask if
04:41and in our if itself, we're going to put here a condition. And those conditions are basically
04:45basic mathematical conditions, such as in our scenario, if a is smaller than zero, and I'm
04:53going to close the round bracket by saying if a is smaller than zero, I'm saying if the
04:57value is smaller than zero, then I want to do something. Now, traditionally with if statements,
05:02there's two ways of doing it. If you're only going to do one line, you don't have to open
05:07up a curly bracket and close a curly bracket. And again, those curly brackets, mainly their
05:11main goal is to be able to define where is the start and where is the end of something.
05:16So in this case, if this comes true, we're going to do anything that's inside of between
05:21these opening and closing curly brackets. Now, you could use only one line, but it's
05:27just not encouraged. So we're not going to do that, we could without using a curly bracket,
05:31basically, it will only do one line, it's really prone to errors. And, you know, even though it
05:37adds another couple of lines or a code to add the curly brackets, just always add the curly
05:41brackets, it's going to make your life easier. Alright, so if our value is smaller than zero,
05:45what do we want to do, we want to take that value. And we want to say that the value equals
05:50now a times minus one, we want to take that value that was there before, and basically
05:56duplicate it by minus one, making it positive. Now, even though we're sending a negative value
06:02right now in a, if we refresh our screen, again, we're going to get a positive value.
06:08And same is true if we send and be, which was already positive, then it won't change it.
06:13And we're going to get the value of 32.

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!