Skip to content

024 Merging Global and Local Scopes

petro edited this page Aug 8, 2013 · 1 revision

Merging Global and Local Scopes

S# also provides a possibility importing a set of global variables into the local scope of a function. This is achieved by means of global() statement used within function definition.

Example:

myVariable1 = 10;
myVariable2 = "hello";

function MyFunction() global (myVariable1, myVariable2)
{
  myVariable1++;
  myVariable2+= " world!";
  Console.WriteLine(myVariable1);
  Console.WriteLine(myVariable2);
}

The example above will provide the following output when executed:

11
hello world!

Note: After variables from global scope are merged into the local one for "MyFunction" function it is not possible to declare local variables with the same names. It is recommended to use "global()" statement for functions which are intended to manipulate static (global) variables primarily.