Minor fixes in and cleaning up typoes in license

This commit is contained in:
danielyxie 2018-03-03 15:22:50 -06:00
parent 2c68d17bc3
commit 99d774cf5a
7 changed files with 88 additions and 80 deletions

Binary file not shown.

Binary file not shown.

@ -900,21 +900,47 @@ Functions should have some return value. Here is an example of defining and usin
return res;
}
print(sum([1, 2, 3, 4, 5]));
print(sum([1, 10]));
print(sum([1, 2, 3, 4, 5])); //Prints 15
print(sum([1, 10])); //Prints 11
The example above prints the following in its log::
For those with experience in other languages, especially Javascript, it may be important to note that
function declarations are not hoisted and must be declared BEFORE you use them.
For example, the following will cause an error saying `variable hello not defined`::
15
11
print(hello());
function hello() {
return "world";
}
The following will work fine::
function hello() {
return "world";
}
print(hello()); //Prints out "world"
**Note about variable scope in functions:**
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
Any changes to "global" variables will only be applied locally to the function. This also means that any variable that is first defined inside a
function will NOT be accessible outside of the function.
Any changes to "global" variables will only be applied locally to the function.
For example, the following code::
The following example shows that any change to a "global" variable
from inside a function only applies in the function's local scope::
function foo() {
i = 5;
return "foo";
}
i = 0;
print(i); //Prints 0
foo();
print(i); //Prints 0
Furthermore, this also means that any variable that is first defined inside a
function will NOT be accessible outside of the function as shown in the following example::
function sum(values) {
res = 0;
@ -933,22 +959,6 @@ results in the following runtime error::
Args:[]
variable res not defined
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
function foo() {
i = 5;
return "foo";
}
i = 0;
print(i);
foo();
print(i);
Results in the following log::
0
0
**Other Notes about creating your own functions:**

@ -1509,20 +1509,46 @@ false if the player clicks "No". The script's execution is halted unti
<span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="p">}</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]));</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">]));</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]));</span> <span class="o">//</span><span class="n">Prints</span> <span class="mi">15</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">]));</span> <span class="o">//</span><span class="n">Prints</span> <span class="mi">11</span>
</pre></div>
</div>
<p>The example above prints the following in its log:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="mi">15</span>
<span class="mi">11</span>
<p>For those with experience in other languages, especially Javascript, it may be important to note that
function declarations are not hoisted and must be declared BEFORE you use them.
For example, the following will cause an error saying <cite>variable hello not defined</cite>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">hello</span><span class="p">());</span>
<span class="n">function</span> <span class="n">hello</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="s2">&quot;world&quot;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The following will work fine:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">function</span> <span class="n">hello</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="s2">&quot;world&quot;</span><span class="p">;</span>
<span class="p">}</span>
<span class="nb">print</span><span class="p">(</span><span class="n">hello</span><span class="p">());</span> <span class="o">//</span><span class="n">Prints</span> <span class="n">out</span> <span class="s2">&quot;world&quot;</span>
</pre></div>
</div>
<p><strong>Note about variable scope in functions:</strong></p>
<p>Functions can access &quot;global&quot; variables declared outside of the function's scope. However, they cannot change the value of any &quot;global&quot; variables.
Any changes to &quot;global&quot; variables will only be applied locally to the function. This also means that any variable that is first defined inside a
function will NOT be accessible outside of the function.</p>
<p>For example, the following code:</p>
Any changes to &quot;global&quot; variables will only be applied locally to the function.</p>
<p>The following example shows that any change to a &quot;global&quot; variable
from inside a function only applies in the function's local scope:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">function</span> <span class="n">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="k">return</span> <span class="s2">&quot;foo&quot;</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="o">//</span><span class="n">Prints</span> <span class="mi">0</span>
<span class="n">foo</span><span class="p">();</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="o">//</span><span class="n">Prints</span> <span class="mi">0</span>
</pre></div>
</div>
<p>Furthermore, this also means that any variable that is first defined inside a
function will NOT be accessible outside of the function as shown in the following example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">function</span> <span class="nb">sum</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="p">{</span>
<span class="n">res</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">values</span><span class="o">.</span><span class="n">length</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
@ -1541,23 +1567,6 @@ function will NOT be accessible outside of the function.</p>
<span class="n">variable</span> <span class="n">res</span> <span class="ow">not</span> <span class="n">defined</span>
</pre></div>
</div>
<p>The following example shows that any change to &quot;global&quot; variable inside a function only applies in the function's local scope:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">function</span> <span class="n">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="k">return</span> <span class="s2">&quot;foo&quot;</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="n">foo</span><span class="p">();</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
</pre></div>
</div>
<p>Results in the following log:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="mi">0</span>
<span class="mi">0</span>
</pre></div>
</div>
<p><strong>Other Notes about creating your own functions:</strong></p>
<p>Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function
you create in functions such as <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">Array.sort()</a> (not yet at least, I'll try to make it work in the future).</p>

File diff suppressed because one or more lines are too long

@ -900,15 +900,12 @@ Functions should have some return value. Here is an example of defining and usin
return res;
}
print(sum([1, 2, 3, 4, 5]));
print(sum([1, 10]));
print(sum([1, 2, 3, 4, 5])); //Prints 15
print(sum([1, 10])); //Prints 11
The example above prints the following in its log::
15
11
For those with experience in other languages, especially Javascript it may be important to note that function declarations are not hoisted and must be declared BEFORE you use them.
For those with experience in other languages, especially Javascript, it may be important to note that
function declarations are not hoisted and must be declared BEFORE you use them.
For example, the following will cause an error saying `variable hello not defined`::
print(hello());
@ -916,24 +913,21 @@ For those with experience in other languages, especially Javascript it may be im
return "world";
}
Will cause an error saying `variable hello not defined` whereas::
The following will work fine::
function hello() {
return "world";
}
print(hello());
Will print out
world
print(hello()); //Prints out "world"
**Note about variable scope in functions:**
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
Any changes to "global" variables will only be applied locally to the function.
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
The following example shows that any change to a "global" variable
from inside a function only applies in the function's local scope::
function foo() {
i = 5;
@ -941,16 +935,12 @@ The following example shows that any change to "global" variable inside a functi
}
i = 0;
print(i);
print(i); //Prints 0
foo();
print(i);
print(i); //Prints 0
Results in the following log::
0
0
Furthermore, this also means that any variable that is first defined inside a function will NOT be accessible outside of the function as shown in the following example::
Furthermore, this also means that any variable that is first defined inside a
function will NOT be accessible outside of the function as shown in the following example::
function sum(values) {
res = 0;
@ -970,7 +960,6 @@ results in the following runtime error::
variable res not defined
**Other Notes about creating your own functions:**
Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function

@ -24,7 +24,7 @@ contains or is derived from the Software or any part thereof:
(a) The distributed Software may not be used in any way for commercial gain and must not violate any of the restrictions
set forth in Section 3.
(b) If you have modified the Software, you must cause cause the modified Software to carry prominent notices stating that
(b) If you have modified the Software, you must cause the modified Software to carry prominent notices stating that
you have modified the Software's files. Modifications must not alter or remove any copyright notices in the Software.
(c) The distributed Software must include either a written copy of this License, or a prominent written indication that
the Software is covered by this License and written instructions for printing and/or displaying the copy of the License