Scope

A scope is a region of the program where a defined variable can be accessed, the variable cannot be used outside the scope.

Global variable
Global variables are defined outside a function, usually on top of the program.
Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
We need to use keyword global to declare the global variable before using it in function.
In main() function, we can use global variable directly.

_gIndex=0;//global variable

func set_index(index)
{
    global _gIndex;//declare it is a global variable before using it
    _gIndex=index;//update the value of the global variable
}
main()
{
        print(_gIndex);//print the global variable
        set_index(100);//change the value of global variable
        print(_gIndex);//print the new value
}
Result:
0
100

If we do not declare the global variable, _gIndex in function set_index is a local variable, so we cannot update the global variable:
_gIndex=0;//global variable

func set_index(index)
{
   _gIndex=index;//gIndex is a local variable, so the update does not affect the global variable
}
main()
{
        print(_gIndex);//print the global variable
        set_index(100);//will not change the global variable
        print(_gIndex);//print again
}
Result:
0
0

If we only get the value of global variable, and do not assign new value for it(assignment means new variable), the global declaration is not required:
_gIndex=0;//global variable

func test()
{
    //here we only get the value of _gIndex, and do not update it, so we can omit the declaration
    n=_gIndex+10;
    print(n);
}
main()
{
    test();
}
Result:
10
Basically we should declare global variable before using it, so declaration omitting is not recommended.

We defined a global variable named _g1 in file file1.si, file1.si is included in file2.si, so we can use the global variable _g1 in file2.si:
//file1.si
_g1=0;//define a global variable _g1 in file1.si

//file2.si
#include "file1.si" //include the file1.si
func test()
{
    print(_g1);//print the global variable _g1 in file1.si
}


Local variable
Variables that are declared inside a function are called local variables. They can be used only inside that function. Local variables are not known to functions outside their own.
func test(index)
{
    a=1;//a is a local variable in function test
    b=index;//b is a local variable in function test, index is parameter, it's also a local variable
}
main()
{
        x=0;//local variable
        test(100);
}


Static variable
To reuse the same name in different source files, we can use static keyword.
If we defined a global variable _g1 in file file1.si, and we added static before the name, it means that the global variable _g1 is only available in file file1.si, it cannot be used in any other files.
//file1.si
static _g1=0;//defined a global variable _g1 with static in file1.si

//file2.si
#include "file1.si" //include file1.si
func test()
{
    print(_g1);//error, _g1 cannot be accessed in file2.si, beause it is a static variable in file1.si
}

//file3.si
#include "file1.si" //include file1.si

//we define a global variable with the same name _g1 as the one in file1.si,
//because _g1 in file1.si was added static, a same name global variable is ok here. They are different variables.
_g1=1;

main()
{
    print(_g1);//here we print the _g1 in file3.si
}

static is applied to functions and classes as well.
We can reuse the same name for functions/classes in different files.
static func test()
{
    print("test");
}

static class Point(x, y)
{
    self.x=x;
    self.y=y;
}