×
Create a new article
Write your page title here:
We currently have 277 articles on Waste Of Space Wiki. Type your article name above or create one of the articles listed here!



    Waste Of Space Wiki

    Programming/Staging: Difference between revisions

    Content deleted Content added
    m revised scoring
    m revised quiz
     
    (3 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 valuesame as ofassigning <code>a</code> toa new value (<code>a = 5</code>). AsThis overrides the variableother <code>a</code> waswith alreadya defined,new <code>locala</code>; isequal noto longer5: necessarynot (the3, valuebut isonly justin beingits changedlocal out)scope.
    * <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 that scope block - the top scope block.
    * <code>print(x)</code> prints out '''nil. nil''' represents no value - not as in 0, but as in the '''complete absence of anything'''. This<code>x</code> is equal to nil for the same reason why printing <code>a</code> before this gave 3: <code>x</code> is only defined to have a value within its scope block; as it wasn't defined in any way outside of it, it lacks one.
     
     
    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.
    Line 161:
    |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!"
    print(hello)
    Line 171 ⟶ 172:
    |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">
    Line 192 ⟶ 196:
    - 2, then 3
    || You seem to have mixed up the variables.
    -+ 3, then 5
    +- 3, then 2
    || <code>b</code> is equal to 5 only in the scope block; not outside of it.
    || 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>)
    + 3, then 2
     
    { Complete the missing lines:<syntaxhighlight lang="lua" line="1">
    Line 203 ⟶ 207:
    c = 54
    do
    print(s)
    ...
    ...
    ...
    print(s)
    end
    end
    </syntaxhighlight>
    If the output of the code was '''32''', then '''54''', andthen '''24'''.
     
    |type="{}" coef="4"}
    First missing line: { print(cs) }
    Second missing line: { print(c=24)|s=24|local cs = 24|local cs=24|local s = 24|local s=24 }
    Third missing line: { print(c)s=24| s = 24|local s=24|local s = 24|print(sc) }
    </quiz>
     
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • Jo857294 • 1 day ago
  • 116.111.185.163 • 13 days ago
  • 116.111.185.163 • 13 days ago
  • 116.111.185.163 • 13 days ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.