Symbols and Values

Gamma: Version 4.0
Prev | Chapter 2. Getting Started | Next

# 2.3. Symbols and Values

A symbol is a fundamental notion in Gamma. It is a unique word or name within the program. In this sense all symbols are global in Gamma (although they can have local scope), and all references to a particular symbol will be the exact same Gamma object.

A symbol can contain: lowercase letters, uppercase letters, digits, and underscores.

Symbols can have values so they can be used as variables. For the time being it is probably easier to think of a symbol as a variable. (For other uses of symbols see the Advanced Types and Mechanisms chapter of this guide). The same symbol can have different values depending on the part of the program in which the reference to the symbol is made.

For example:

        Gamma> i = 5;
        5
        Gamma> function MyLocal (n) { local i; for (i = 1; i < n; i++) princ("i = ", i, "\n");}
        (defun ...)
        Gamma> MyLocal (3);
        i = 1
        i = 2
        2
        Gamma> i;
        5

In this example the symbol `i` is created and the value of `5` is assigned to it. Then in the definition of function `MyLocal` the value of `i` is declared to be local> to the function (or to have local scope). The function prints the local values of `i` and returns the result of the last calculation. After `MyLocal` returns, the value of the symbol `i` remains `5`, as defined at the beginning.

Unlike many procedural languages such as C and Pascal, Gamma uses dynamic scoping. That means that if a function defines a local variable, then that variable becomes global to any functions it calls. In the example above, suppose we define `MyScope` in the higher call, or at the global level:

        Gamma>  function MyScope (s) {........}

Then we modify the function `MyLocal` such that now it calls `MyScope`:

        Gamma>  function MyLocalNew (n)
        {
          local i;
          for (i = 1; i < n; i++)
            {
              princ("i = ", i, "\n");
              MyScope(i);
            }
        }

The value of `i` used as an argument for `MyScope` will be the value of the local variable most recently declared in the calling sequence: `MyScope (1)`, `MyScope (2)`, etc. The scope of the variable is thus determined at run time by the order in which functions are called.

Dynamic scoping in Gamma is very different from the convention in C known as lexical scoping, where the scope of a variable is determined according to the syntactic position in a program where it is declared.

Prev | Home | Next
Executable Programs | Up | Basic Data Types and Mechanisms

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.