Sambar Server Documentation

CScript Variable Scoping


Local variables are declared within a function. These are created anew each time the function is called, and destroyed upon returning from the function. Values passed to the function as arguments are treated just like local variables with the following exceptions:
reverse(a, b) { c = a; b = a; a = c; } main() { a = 1; b = 2; // By default, integers are passed by value reverse(a, b); printf("a = %d, b = %d\n", a, b); // a == 1, b == 2 // Here, integers are passed by reference reverse(&a, &b); printf("a = %d, b = %d\n", a, b); // a == 2, b == 1 x = "test1"; y = "test2"; // Strings operate differently -- they're always passed by reference. reverse(x, y); printf("x = %s, y = %s\n", x, y); // x == test2, b == test1 }

Local Variables in Compound Statements

Most examples seen so far have consisted of a single compound statement. The following program demonstrates the declaration of variables within compound statements:
main()
{
    double  x;
    int     flag = 0;

    x = 5.5;
    if(x > 1.5)
    {
        int flag = 1;
        printf("flag = %d, x = %10.5lf\n",flag,x);
    }
    else
    {
        int flag = -1;
	printf("flag = %d, x = %10.5lf\n",flag,x);
    }

    printf("flag = %d, x = %10.5lf\n",flag,x);
}

There are 3 distinct variables called flag in this program. One declared at the start of the program and two others declared within the compound statements associated with the if and else blocks. In C, changing one of the flag variables within a compound statement has no effect on the flag variable declared at the start of the program. CScript does not conform with C scoping of compound statements. In CScript, all of the above flag variables are scoped to the flag variable declard at the start of the program. This is a significant difference from C.

Global Variables

Local variables are declared within a function, and can only be used within that function. When another function is called, all local variables that are needed within that function must be passed to it as arguments. Alternatively, a variable can be declared so it is available to all functions. This is called a global variable.

A global variable is declared as normal, but outside any of the program's functions. This is usually done at the beginning of the program file. The variable is not declared again in the body of the function which accesses it; this would result in a shadow variable which has no relationship to the global variable it replaces in the context of the function in which it is defined.

Static Variables

Another class of local variable is the static type. A static can only be acessed from the function in which it was declared, just like a local variable. The static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static.
static int counter;
Static variables can be initialised as normal, however initializion of the variable takes place only once when the program starts up, not every time the function is called.

Important! Current static variables are not supported in CScript.

© 2001 Sambar Technologies. All rights reserved. Terms of Use.