Scope - First Dabs
Looking deeper into the concept of scope and how they relate to our functions and variables and in the process realizing we learned another concept of nested functions.
Video Transcript
00:00So this whole topic is really about scope.
00:03And one thing that we've been playing around with,
00:06but we didn't say it out loud, is
00:08that we could have multiple functions inside of a function.
00:11So a function could contain functions
00:13as we've been building all of our functions
00:16inside of our main function.
00:18With that said, I'm just going to create a quick function
00:20to illustrate it.
00:21And our quick function will be, say hello,
00:26which would be a function.
00:27It would not return any value.
00:30And it would trace out hello.
00:34Very good.
00:35So far, we have one function inside of our main.
00:37But as we've done before, we could put in another function.
00:41So I could have a function here that says get hello.
00:45And it would return a string.
00:47And instead of tracing out, it could return a value of hello.
00:53OK?
00:54So my say hello function has access
00:58to get hello because they're in the same level.
01:00And when I say they're in the same level,
01:02if I just highlight it, everything
01:04inside of my main function, my say hello function
01:08has access to all the things that are inside of my main.
01:11And beyond.
01:12Whatever function you're in has access
01:14to everything in its level and higher.
01:18Now what does that mean?
01:20So in our scenario, get hello, we
01:22could have just set, instead of tracing hello,
01:24we could have set get hello.
01:27So in our trace, inside of say hello,
01:29we could get our hello message.
01:31So if we run our application now,
01:33we should be seeing our message in our output.
01:36But we're not.
01:37So I'm doing here something wrong, OK?
01:39So let's take one step back.
01:41Oh, well, the reason is we're never calling it, right?
01:44So let's actually call our function.
01:47And to call our function, we want to call our say hello
01:49because the say hello function was never executed.
01:52So let me call out say hello.
01:54And let's run it one more time.
01:56And we're getting our message hello.
01:57So our say hello function had access to our get hello
02:01function.
02:02And the reason was is because it had high level access.
02:07Now if I grab my get hello message
02:09and I would put it into my say hello function,
02:13what would actually happen?
02:15It would still have access to it, right?
02:17Because it's in the same level.
02:19But on the other hand, if I would
02:21try to trace out directly.
02:22And let's run the application one more time
02:25to see that it actually really works.
02:27So our get hello, our say hello has access to it
02:30because it's inside its level, right?
02:32And if we run our application again,
02:34we'll see that hello comes back.
02:35It finds the get hello.
02:37On the other hand, if I take our function back out
02:41and instead of tracing say hello,
02:43I want to trace out get hello.
02:46If I run this now, will this have access?
02:49Yes, it would because it's in the same level, right?
02:52So if I run it out again, I'm gonna say hello.
02:56But if I grab the same function
02:58and I put it inside another function,
03:00now get hello doesn't have any more access to,
03:04well, our trace doesn't have access to get hello
03:06because it has only access to say hello
03:10and anything that would be higher from it, right?
03:13You have access to things that are in your level
03:15and higher up, you can't dig any deeper inside of it.
03:19So if we run our application again,
03:20it's gonna tell us that it can't find the get hello.
03:24And the reason is is because we have a scope.
03:27Functions have scope, everything in Flash has scope.
03:30In a way, you need to know where you are
03:33and based on where you are,
03:34you would know what you could communicate with.
03:36With that said, as we get back into building classes,
03:40we're gonna revisit this concept
03:42and dig a little bit deeper into the concept of scope.
03:46We've seen scope with functions,
03:48but the same is true with variables
03:50and let's see what does that mean.
03:52Okay, so let me just comment out this whole block of code
03:55and let's create a new block of code
03:57and use it with variables.
03:59So first off, if I create here a variable
04:01and I call it, let's call it message,
04:07I can be seen.
04:10Now, if I just try to trace out directly my message,
04:13do you think we'd be able to access it?
04:15Definitely, we've done it many times before, right?
04:18So our trace can be seen.
04:21What if we, instead of tracing directly,
04:24we would type function, trace message,
04:31right, it's a void function
04:32and we wanna trace out message.
04:35Would our message still be seen?
04:37Let's call our trace message
04:41and we could still see our message.
04:42So let's write that down again.
04:45So if I trace our message directly, right,
04:50works, works as well, right?
05:01Let's try one more trace message,
05:03one more trace message
05:08and let's uncomment it
05:11and let's do a trace message two.
05:13And in trace message two,
05:15instead of sending,
05:17instead of our variable being,
05:19we're gonna pass a variable as well
05:21and we're gonna pass a variable of, hey you,
05:27and in hey you,
05:28we're gonna give it a message variable right here.
05:31And see what we're actually doing here?
05:33We're blocking access
05:34because now in our trace message two,
05:38it's receiving a variable of message
05:40and thus there's a new scope inside of our trace message,
05:43right, as we work with our functions.
05:46And that is blocking our access
05:49to our variable that's externally.
05:51So if we run our application again,
05:52we'll see our third value message
05:55is not the same message variable
05:56as our main function had.
05:59And if we run this again,
06:00we'll see that our first two are
06:02are finding our variable
06:03because there's,
06:04the reason is if we look at this function,
06:06when you trace messages,
06:07basically first of all,
06:07it checks to see if in its scope
06:09there is a message variable.
06:12When it doesn't find it,
06:13it goes one level up
06:14and it searches for,
06:16and it finds it.
06:17Okay, so it always,
06:18the highest priority is the closest to you.
06:20And now when I created a new function
06:22and I had a new variable inside of it,
06:24which was message,
06:25that message has a higher priority.
06:28And it's kind of like overriding the access
06:30to our original message string.
06:33We're gonna revisit this concept
06:35and look a little bit deeper into it.
06:37And when could we access these things
06:41as we dig a little bit deeper into scope
06:44when we get a bit more comfortable
06:46with objects and classes
06:48and the world of object oriented programming.
06:51And everywhere you are,
06:52if you're in here,
06:53your scope is your function.
06:55Now, it should have access
06:57to things that are higher up in the scope,
07:00but its main reference is its scope.
07:02So if you're in your trace right here
07:04and it has a variable,
07:05that is the top priority.
07:07Same thing is if,
07:09same thing is if we had another function of,
07:13let's say,
07:15for example,
07:16we would have another function in here
07:18that would be called trace.
07:20Then if we had another function in here
07:22that would be called trace
07:22that our internal trace would probably
07:24override our external trace.
07:25Now I'm not gonna test that out
07:27because trace is a built in function
07:29and I don't wanna really mess around with it,
07:30but if we had our own unique functions right here,
07:35then it would override even if they had the same name.
07:40In other words,
07:41let's make it a little bit more clear.
07:43So if I'm gonna call,
07:45if I'm gonna trace message three
07:49and then trace message three,
07:51I'm gonna send,
07:53hello,
07:56and I'm gonna just create a function right here
08:00and I'm gonna call it trace message three,
08:02but instead of calling trace,
08:03I'm gonna call trace message two, right?
08:06Now hello should work, right?
08:07Cause we're sending the value,
08:09we're just passing it onwards
08:11and we should get our last,
08:12our fourth output to be hello and we are.
08:17Now what would happen if we would grab our trace message two
08:21and create another function
08:24inside of our trace message three
08:26that is called trace message two.
08:28And let's append to our trace something.
08:30So let's say we're gonna put here some extra things.
08:35If we run our application again,
08:36we're gonna see that this is the function
08:38that will be called and not the external function.
08:40Why?
08:41Because the scope inside of our trace message three
08:44first finds a function called trace message two
08:47inside of its scope.
08:48So it doesn't even look up one step up
08:50to see if there is something higher up, okay?
08:54Now these are really important things to get comfortable with
08:57although they're kind of weird
08:58and as we dig a little bit more in our code in the future
09:02where it's gonna get much, much more clear.
09:04So kind of we tackled two subjects here
09:07which you could create functions inside of functions.
09:10And at the same time we talked about scope
09:12and we talked about how that connects
09:14to scopes of functions
09:15and how that connects to scope of variables.
