Variables Contents

# Variables

One defines variables with the define function:

```    (define x 13)
```

The above expression creates a variable named x in the current scope and initializes it to the value 13. If the initializer is missing:

```    (define y)
```

an uninitialized variable error is generated:

```    EXCEPTION: uninitializedVariable
variable y is uninitialized
```

## Functions

A function definition is another way to create a variable. There are two ways to define a function. The first is through a regular variable definition, where the initializer is a lambda expression:

```    (define square (lambda (x) (* x x)))

(square 3)
-> 9

(inspect square)
-> square is <function anonymous(x)>
```

The above expression defines a function that returns the square of its argument and emphasizes that the name of a function is simply a variable that is bound to a lambda (i.e. an anonymous function). The second method uses a special syntax:

```    (define (square x) (* x x))

(inspect square)
-> square is <function square(x)>
```

In either case, a variable is created and bound to some entity that knows how to compute values.

## Scopes, Environments and Objects

When one defines a variable, the variable name and value are inserted into the current scope. In actuality, the name-value pair is stored in a table called an environment. For the predefined variable this, its value is the current scope or environment. For example, consider the following interaction:

```    (define n 10)

(ppTable this)
-> <object 8393>
__label  : environment
__context  : <environment 4495>
__level  : 0
__constructor  : nil
this  : <environment 8393>
n  : 10
```

The pp function will print out the list of variables and their values for the given environment. Among other information stored in the current environment, we see an entry for n and its value is indeed 10.

The Scam object system is based upon environments. We will learn about objects in a later chapter.

## Defining Variables Programatically

The function addSymbol is used to define variables on the fly. For example, to define a variable named x in the current scope and to initialized it to 13, one might use the following expression:

```    (addSymbol 'x 13 this)
```

You can also define functions this way:

```    (addSymbol 'square (lambda (x) (* x x)) this)
```

Since addSymbol evaluates all its arguments, the first argument can be any expression that resolves to a symbol, the second argument can be any expression that resolves to an appropriate value, and the third argument can be any expression that resolves to an environment or object.

## Variable naming

Unlike many languages, Scam is quite liberal in regards to legal variable names. A variable can't begin with any of the these characters: `0123456789;,`'"()` nor whitespace and cannot contain any of these characters: `;,`'"()` nor whitespace. Typically, variable names start with a letter or underscore, but they do not have to. This flexibility allows Scam programmers to easily define new functions that have appropriate names. Here is a function that increments the value of its argument:

```    (define (+1 n) (+ n 1))
```

While Scam lets you name variables in wild ways:

```    (define \$#1_2!3iiiiii@ 7)
```

you should temper your creativity if it gets out of hand. While the name `\$#1_2!3iiiiii@` is a perfectly good variable name from Scam's point of view, it is a particularly poor name from the point of making your Scam programs readable by you and others. It is important that your variable names reflect their purpose.

lusth@cs.ua.edu

 Variables Contents