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

Calling Functions

The first step in understanding how to work with functions is to understand how to use them and more impotently to understand what they are. Functions in the quickest summery is away for us to create an event/action, while variables store information functions do something.

Video Transcript

00:00All right, so let me comment down this amount of code for a second here.
00:03And by the way, I'm using Dreamweaver, so there's a lot of shortcuts that I could use,
00:07but I'm not going to use them. I'm just going to try to stick to the most basic operation.
00:13As you can notice, I moved from JavaScript for the last video to a new program,
00:17just so you could see how it works in all these different applications.
00:20All right. So next up ahead, I want to show you the syntax basically of a function,
00:25even though you've seen it in action, just to make sure that you get it.
00:28Basically, you call the function name. You give it a name just as you would give a variable.
00:34There are a lot of functions that are built in, just like the alert one,
00:37and we're going to meet a lot of them. And you're going to meet a lot of them as you start coding.
00:42The next step that you do, it's not enough to call a function, you have to let it know
00:45that we want to trigger it. Because basically, a function is very similar to a variable,
00:49you could call it, or you could trigger it. A variable automatically, when you call it,
00:54you trigger it, basically, because you could fetch the information that's inside of it.
00:59A function, on the other hand, is more like some sort of a container,
01:02where you could pass that container around, or look at the container,
01:05or you could say, I want to actually activate this container.
01:09To activate the function, to activate that container, you open and close a round bracket.
01:16And then you end your line with the semicolon, you always do that,
01:20that just helps JavaScript know that you finished with your operation.
01:25By the way, you could also pass parameters, just as much as we've passed one parameter
01:29with the alert, we could have passed a couple of parameters,
01:32and basically separating the parameters with a comma.
01:35And a parameter is basically just information for that function,
01:38so that function could then do something with it.
01:41All right, so I want us to see a function in action, a real function in action,
01:46which we've seen already, the alert in action.
01:49So I want to show you another one. So first of all, I'm going to create another variable,
01:53and I'm going to call this big, because I'm going to put here the biggest number.
01:58And by the way, I'm getting here a blue value,
02:00which basically tells me that this might be a reserved word,
02:04or it might be something that will be in the future reserved word.
02:08Reserved words are basically keywords that a programming language needs,
02:12such as the var, we can't use var, because var is used to define something.
02:17We can't use, there's a lot of different keywords like that.
02:19So whenever if you're using an editor that is smart,
02:21and you see these colors, just avoid those keywords.
02:24You don't have to memorize them by heart.
02:27But one great way to avoid them is by always giving your variables
02:31a name that starts with their type.
02:32So in our scenario, if I call it n big,
02:35there would never be a reserved keyword that would be called n big.
02:39So we solved that issue.
02:40All right, so let's see, let's see a function in action.
02:43And we're actually going to see something.
02:44And before we do this, let me show you just one more step.
02:47So until now, we said there are such things as functions,
02:50we've worked with a couple of them, such as the alert, and the,
02:54well, I can't remember which other ones we use.
02:58Now, the next step that we want to do is there,
03:00there are functions that are grouped up into buckets.
03:03Now, sometimes it's inside of an object,
03:06which we're going to learn what an object is in a couple of videos.
03:08And sometimes it's inside of a class reference,
03:11which we'll learn about that in a future video as well.
03:14But I want you to just get the basic syntax of how it actually works.
03:18You basically provide here the, let's call it container name.
03:24Then you use a dot, which is basically how JavaScript works,
03:28use a dot syntax to access and go inside of that container,
03:32which we're going to understand a little bit more about that very, very soon.
03:35And then we could call that function name in the exact same way as we've done it before.
03:40All right, now that we got that out of the way,
03:42let's see it in action using some a little bit of math.
03:45So there's actually a built-in library of mathematical operations
03:49inside a JavaScript that is called math.
03:51And to access it, we just type math with an uppercase M.
03:56And then when we press a dot, if you work in Dreamweaver,
03:59you're going to start seeing all these different things you could do with math.
04:04So I'm just going to start with the most basic one, which is abs,
04:06which is basically creates a positive value.
04:10You put inside a number no matter what type of number,
04:13and that number will come back as a positive number.
04:16So if I put here minus 20, and then we're going to do an alert right after that for that n big.
04:23And again, just notice we use your two functions.
04:26One function was inside of a container, and that container was the math container.
04:30And we called the function abs inside of the math container.
04:34It's basically just organized by categories.
04:37And then we passed in one parameter, which was minus 20.
04:41Now, just to make things a little bit interesting,
04:43obviously n big is not the right name for this.
04:46So let's call this n abs.
04:49n abs.
04:52And I just want to create here a couple of variables.
04:54OK, so I'm going to create here a variable called na,
04:57which is going to be a number, and its value is going to be minus 20.
05:01I'm going to create a nb, and its value is going to be 32.
05:06All right, so we have two variables.
05:08And now, I just want to show you that although we sent it,
05:10and we called our math abs just sending a direct number,
05:13we could have sent also a variable.
05:16Why would we want to do that?
05:17Because maybe sometimes those variables are created dynamically,
05:20or they're dependent on other things that we don't want to use a hard coded value.
05:26And a lot of times in programming, the less hard coded values you use,
05:29the more control you have over your application in general.
05:32And that's just a matter of getting used to.
05:34So I just want to make sure that I call my,
05:37I see that I have an error here,
05:38because I always start variables with a lowercase n,
05:42if it's a number.
05:42But I start with lower cases and not upper cases.
05:46All right, so and it's camel case, so it's nabs.
05:49So there we go.
05:49So let's run this.
05:51Basically, what we hope that will happen is,
05:53I just saved the file.
05:54What we hope will happen is na, which equals minus 20.
05:57When we send it into the math abs, it should return 20.
06:01So our nabs should equal 20.
06:03So I'm going to click here on the live view and Dreamweaver
06:06to be able to see.
06:06And here we go.
06:07We just got our 20, which is exactly what we would hope to get.
06:11There we go.
06:12So we're making some progress.
06:14And now we've seen basically, we already
06:16seen how to work with a function that's a standalone one.
06:19And we've seen how to work with a function that's
06:21inside of a container.
06:22And I'm not going to give it names yet,
06:23because we're going to give those names later on
06:25as we learn about objects.
06:28But if you really need to know,
06:29the name of a function that belongs to an object
06:32is called a method.
06:33And really, the only difference is,
06:35is that one function stands in the air,
06:37while the other function has a relationship with something,
06:40such as this container.
06:42All right.
06:42So in our case, by the way, just throwing names,
06:46if you don't know about it, then just don't worry about it.
06:49If it sounds complicated, just ignore what I'm saying now.
06:52The abs function in our scenario is actually a static function
06:55because it doesn't really belong to an object.
06:58It's just wrapped around inside of some sort of property,
07:02some sort of class, that basically enables us
07:04to access all these different functions.
07:06So it's a static function.
07:07But for our state, for our need, we could just call it functions.
07:13All right.
07:13Let's move on to the next step.
07:15The next step for us is actually to create our own functions.

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!