From 6713dca1d62b3edf601a8793effb54a0b36ab1d1 Mon Sep 17 00:00:00 2001 From: JJ Date: Tue, 20 Feb 2018 14:44:28 -0500 Subject: [PATCH 1/2] Custom function scope adjustments Some adjustments made to the function scope documentation to make it easier for people to understand. This came up in Discord where some of us understood it but others did not and I think the way I changed it will make it a little cleaner --- doc/source/netscriptfunctions.rst | 37 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 8661ee953..8078d853f 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -864,10 +864,26 @@ The example above prints the following in its log:: **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 "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 + +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; @@ -886,22 +902,7 @@ 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:** From f99b897ac45fd5223791d3109d623db873f39bcc Mon Sep 17 00:00:00 2001 From: JJ Date: Tue, 20 Feb 2018 14:56:18 -0500 Subject: [PATCH 2/2] Update netscriptfunctions.rst --- doc/source/netscriptfunctions.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 8078d853f..f5114f4cf 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -861,6 +861,26 @@ 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. + + print(hello()); + + function hello() { + return "world"; + } + +Will cause an error saying `variable hello not defined` whereas:: + + function hello() { + return "world"; + } + + print(hello()); + +Will print 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.