×
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 and -> then
    m revised quiz
     
    (2 intermediate revisions by one other user not shown)
    Line 124: Line 124:
    print(a) --prints out 3
    print(a) --prints out 3
    local x = 4
    local x = 4
    a = 5
    local a = 5
    print(x) -- 4
    print(x) -- 4
    print(a) -- 5
    print(a) -- 5
    Line 133: Line 133:
    </syntaxhighlight>
    </syntaxhighlight>


    * <code>local a = 3</code> sets a to be equal to 3, as you should know by now
    * <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>
    ** <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.
    *** 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: 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>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 x = 4</code> ...
    * <code>a = 5</code> sets the value of <code>a</code> to 5. As the variable <code>a</code> was already defined, <code>local</code> is no longer necessary (the value is just being changed out).
    * <code>local a = 5</code> sets <code>a</code> to be equal to 5. This is '''<u>NOT</u>''' the same as assigning <code>a</code> a new value (<code>a = 5</code>). This overrides the other <code>a</code> with a new <code>a</code>; equal to 5: not 3, but only in its local scope.
    * <code>print(x)</code> prints out 4 as this is what x was defined as, at least '''in the current 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 a was defined to be 5 '''in this 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> is equal to 3: what it is defined to be in that scope block - the top scope block.
    * <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 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 is 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; outside of it, it lacks one.
    * <code>print(x)</code> prints out '''nil. nil''' represents no value - not as in 0, but as in the '''complete absence of anything'''. <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: Line 151:
    |type="()"}
    |type="()"}
    - a = 4
    - a = 4
    || This defines <code>a</code> globally - not locally.
    || This defines <code>a</code> globally - not locally (or reassigns a to some other value).
    - do a = 4 end
    - do a = 4 end
    || This defines <code>a</code> globally (not locally), and within a scope block.
    || This defines <code>a</code> globally (not locally), and within a scope block.
    Line 161: Line 161:
    |type="()"}
    |type="()"}
    + TRUE
    + TRUE
    || Yup! Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes
    - FALSE
    - FALSE
    || Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes
    || Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes


    {What will the following code print? <syntaxhighlight lang="lua" line="1">
    {What will the following code print out? <syntaxhighlight lang="lua" line="1">
    local hello = "Hello world!"
    local hello = "Hello world!"
    print(hello)
    print(hello)
    Line 171: Line 172:
    |type="()" coef="2"}
    |type="()" coef="2"}
    - hello
    - 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!"
    - "Hello world!"
    || "Hello world" is indeed what is fed into the print function, but not what is printed out
    - 'hello'
    - '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">
    {Consider the following code: <syntaxhighlight lang="lua" line="1">
    Line 192: Line 196:
    - 2, then 3
    - 2, then 3
    || You seem to have mixed up the variables.
    || You seem to have mixed up the variables.
    - 3, then 5
    + 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">
    { Complete the missing lines:<syntaxhighlight lang="lua" line="1">
    Line 203: Line 207:
    c = 54
    c = 54
    do
    do
    print(s)
    ...
    ...
    ...
    ...
    ...
    ...
    print(s)
    end
    end
    end
    end
    </syntaxhighlight>
    </syntaxhighlight>
    If the output of the code was '''54''', then '''24'''.
    If the output of the code was '''32''', then '''54''', then '''24'''.


    |type="{}" coef="4"}
    |type="{}" coef="4"}
    First missing line: { print(c) }
    First missing line: { print(s) }
    Second missing line: { c=24|s=24|local c = 24|local c=24|local s = 24|local s=24 }
    Second missing line: { print(c)|s=24| s = 24|local s=24|local s = 24 }
    Third missing line: { print(c)|print(s) }
    Third missing line: { s=24| s = 24|local s=24|local s = 24|print(c) }
    </quiz>
    </quiz>


    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

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