m
revised quiz
m (order change for correct questions) |
Iforgoragain (talk | contribs) m (revised quiz) |
||
(5 intermediate revisions by one other user not shown) | |||
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 151:
|type="()"}
- a = 4
|| This defines <code>a</code> globally - not locally (or reassigns a to some other value).
- do a = 4 end
|| This defines <code>a</code> globally (not locally), and within a scope block.
+ local a = 4
- local a = "4"
|| This defines <code>a</code> locally, but as "4" (a string), not 4 (a number).
{Defining variables in the local scope is always preferred
|type="()"}
+ TRUE
|| Yup! Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes
- FALSE
|| Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes
{What will the following code print out? <syntaxhighlight lang="lua" line="1">
local hello = "Hello world!"
</syntaxhighlight>
|type="()" coef="2"}
- hello
|| <code>hello</code> is a variable, so what's actually being fed into the print function is its value - "Hello world"
+ Hello world!
- "Hello world!"
|| "Hello world" is indeed what is fed into the print function, but not what is printed out
- 'hello'
|| <code>hello</code> is a variable, so what's actually being fed into the print function is its value - "Hello world"
{Consider the following code: <syntaxhighlight lang="lua" line="1">
local b = 2
local
do
b = 5
print(
end
Line 172 ⟶ 191:
</syntaxhighlight>
The code will print out:
|type="()" coef="2"}
- 5, then 2
|| You seem to have mixed up the variables.
- 2, then 3
|| You seem to have mixed up the variables.
- 3, then 5▼
+ 3, then
|| Scope isn't relevant here as it's just reassigning the value of a variable - and b is defined in the top scope. To have the code print this out, <code>b = 5</code> would have to be replaced with <code>local b = 5</code> (because when we have that local at the beginning - <b>scope becomes relevant</b>)
{ Complete the missing lines:<syntaxhighlight lang="lua" line="1">
Line 185 ⟶ 207:
c = 54
do
▲ print(s)
...
...
...
print(s)
end
end
</syntaxhighlight>
If the output of the code was '''32''', then '''54''',
|type="{}" coef="4"}
First missing line: { print(
Second missing line: { print(c
Third missing line: {
</quiz>
|