Robin supports what is called ‘scoping’ in the programming world.
What is Scope?
In computer programming, the scope of a name binding – an association of a name to an entity,
such as a variable – is the region of a computer program where the binding is valid:
where the name can be used to refer to the entity.
Such a region is referred to as a scope block. - via Wikipedia
In simpler words: scope is the ‘range’ in which a variable is valid/can be seen or accessed.
In Robin, variables fall into two categories: local and global
By default, all variables that are declared in the outer levels of a Robin script are global, which means that they can be accessed by anywhere inside the script.
Another thing to keep in mind regarding global variables is that their value is consistent and changes affect the value throughout its existence.
The variable can be utilized inside functions we create.
Our script is checked and runs without any problems.
We declared a variable and given it a value and we saw that it can be accessed from anywhere inside our script. Let’s see another example with value change.
Now we create a function that is going to utilize the variable internally.
It is going to print its value, then change it and print it again.
After that we print the variable outside the function and see it has retained its value.
Recapping, we created a numeric variable and gave it an initial value.
Then we printed the variable and as expected it was the one we gave it at first.
We created and called a function that calls the variable and prints it.
At first the initial value remains the same.
Then it changes the value and prints it again.
As expected the value is now the last one given.
After that we print again the variable.
Note that we are now again in the main body of our script.
The variable has retained the value it was given inside the previously called function.
Local Scoping is used inside functions in Robin.
Every variable that is declared inside a function is local to it by default.
That means that this variable can’t be accessed by any level above.
The localVariable variable is local to the function:
In order to utilize the output of a function such as the previous one, we must explicitly declare a new variable to store it (the output).
By doing so we are practically creating a new global variable to store the output of the function, in this case we are storing the value contained in ‘localVariable’ into the global variable ‘SampleFunction2Output’:
Note: If we explicitly tried to utilize the variable “localVariable” straight away we would get the following error:
This is because its scope is local. It is only visible in the function it was declared.
Global variables in functions
Variables declared inside functions can be of global scope if their name is preceded by the two following characters “g:”.
This means that the variable is of global scope and can be accessed from anywhere in your script.
Let’s repeat the same example as before but with an internal variable declared as global:
Note that this time we don’t have to explicitly declare a new global variable.
He have already declared the internal function variable as global.
Screenshot(s) of the whole script:
Useful documentation links:
GitHub: Scope example