Between Functions and Variables
Showing the position of a function doesn't matter while the position of a variable does.
Video Transcript
00:00Alright, so I started to talk about this in the last video, but I didn't really say it because I thought it was a bit too early.
00:07Let's look at something about variables that are different than functions.
00:12It's a bit of a reminder as well.
00:15If I create a variable, and let's say I call it num,
00:18it will be a number, and even if I put a value into it, it really doesn't matter.
00:23If I try to trace out before I've created the number,
00:27if I try to trace out that number, look what will happen.
00:32The value that would come back would be none.
00:34And it wouldn't generate an error, right?
00:37If we didn't define our variable and we would trace it out,
00:40it would give us an error telling us that it's trying to find something that doesn't exist.
00:44Now, the reason is, is this.
00:47Flash, before it runs the functions, before it runs, and this is a function,
00:52before it runs any function or any object,
00:55it first collects all the variables that are inside of that function and defines them.
00:59It doesn't put any value into them, but it defines them.
01:03Thus, if we call it num on line 6, even though it wasn't created yet,
01:07the definition itself was created.
01:09It's as if we defined it in the beginning,
01:15and then append the value afterwards.
01:19This is the exact same thing.
01:21And the reason is, Flash by default is going to do that,
01:23even if you define your variables after you call your trees.
01:27Only this is quite confusing, right?
01:29So you probably wouldn't want to do this.
01:31Now, on the other hand, with functions, they work very similarly.
01:36But when they're defined, they're also accessible.
01:39So now, if I would call my abs, and I would run it before the function was defined,
01:45and I run the application, everything should be fine.
01:48And the reason is, Flash is founded inside of our scope.
01:52And this is our scope.
01:53I'm going to call that scope.
01:54We're inside of our function.
01:56And it found inside of our scope the abs function.
02:00So it defined it.
02:01It defined it way before it started actually executing code.
02:05And now that we started executing the code, it then found it.
02:08So it doesn't really matter where you put your function.
02:11If you put it before or you put it after, it will find it.
02:14So the functions, their order is not important.
02:17It's not important where you position them for them to become accessible.
02:22Variables, on the other hand, because they're defined, no value is put into them.
02:27And as no value is put into them, it could create really wonky behavior.
02:31So you wouldn't want to do that really.
02:33You would want to avoid those scenarios.
02:35And usually, you'd want to define all your variables in the top.
02:39But because functions, as we said, are kind of, they're not linear.
02:44While variables are something that's linear.
02:46There's line one, line two, line three.
02:48Functions kind of break that linearity because they create an isolated
02:52bucket of information, which then could be reused.
02:55I don't have to call abs only once.
02:57I could call it five times.
02:59And it would just execute that function five times.
03:02That function could happen many times.
03:04We could execute that function many times.
03:06So if we call that function a few times, it would just be called a few times.
03:10So that's an important differentiation between variables and our functions.
03:16And although we said behind the hood, functions are variables as well.
03:20But they're a unique type of variable.
03:22One that we don't define with the var.
03:25We define our functions in the way we've seen them.
03:27So far, we defined them as functions without defining them as variables.
03:32And although we're not going to do that right now,
03:35we're not going to define our functions as variables.
03:37There is a way to do that.
03:39And we're going to look into those details later on.
03:42But for now, we're just going to work with these type of functions,
03:46with these structures, which are similar to our constructor function,
03:51the one that started our application.
03:53And these type of functions have a name.
03:55They're called function statements.
03:58So function statements are predefined before even the application starts running,
04:03before our main function right here starts executing.
04:07It first finds all the variables.
04:09It finds all the functions.
04:11It tests them out.
04:12It makes sure that it knows what they are.
04:14It doesn't put any value into the variables.
04:16But then it starts executing.
04:18So this needs to be clear, because if not,
04:21you'll find here and there weird behaviors.
04:23And it'll be confusing if you don't get that differentiation.
04:26And it's clear to you.
