Content deleted Content added
m and -> then |
m whoops |
||
Line 124:
print(a) --prints out 3
local x = 4
local a = 5
print(x) -- 4
print(a) -- 5
Line 133:
</syntaxhighlight>
* <code>local a = 3</code> sets <code>a</code> to be equal to 3, as you should know by now.
** <code>local</code> sets the scope of the variable to local. This means that the variable's binding is only valid within its scope block or in scope blocks within it's scope block. Imagine scope blocks as the folders on your computer (but not quite). Say you have a home folder and a documents folder, both with some files inside of them. If you're in the home folder, you can only access the files inside of it. You'll only be able to see the files within the documents folder if you go into it. However, unlike with real folders, if you're in the documents folder in this case you'll also be able to see everything within the home folder. If scope still confuses you, just know that you should always define variables by using <code>local (...) = (...)</code>
*** The other option is setting the scope of a variable to global. To set a variable's scope to global, just don't use <code>local</code> before it. This is equivalent to defining a local variable at the top scope (<code>local a = 3</code> in the above example). Local variables should be preferred whenever possible.
Line 139:
* <code>print(a)</code> prints out the value of a. As <code>a</code> already had its value assigned in a higher scope, <code>a</code> is (also) equal to 3 here.
* <code>local x = 4</code> ...
* <code>local a = 5</code> sets <code>a</code> to be equal to 5. This is '''<u>NOT</u>''' the
* <code>print(x)</code> prints out 4 as this is what x was defined as, at least '''in the current scope'''.
* <code>print(a)</code> now prints out 5, as <code>a</code> was defined to be 5 '''in this scope.'''
* <code>print(a)</code>, now outside the scope block, prints out 3 because <code>a</code> is only equal to 5 within that one specific scope block. Outside of it, <code>a</code> refers to the original <code>a</code> and is equal to 3: what it is defined to be in
* <code>print(x)</code> prints out '''nil. nil''' represents no value - not as in 0, but as in the '''complete absence of anything'''.
Line 192:
- 2, then 3
|| You seem to have mixed up the variables.
|| Scope doesn't apply here as it's just reassigning the value of a variable - and b is defined in the top scope.
▲+ 3, then 2
{ Complete the missing lines:<syntaxhighlight lang="lua" line="1">
|