# 9.5 Variables

## Example program

Again, we reprint the example program:

```student(Lisa, 5).
student(Martin, 3).
student(John, 3).
student(Edward, 7).
```

## Using a variable

Consider the following query:

`?- student(Lisa, X).`

`{X = 5}`

What we have just seen is an example of the usage of a variable in a query. The variable "X" was used to ask which semester Lisa was in.

## What is a variable?

A variable is a piece of data whose value we do not know. It will be remembered that variables are a kind of term. This means that wherever we can use a term, we can also use a variable.

## How did Prolog find the answer?

To answer the query just given, Prolog tried to match the goal with all of the facts of the database. It turned out that only the first fact matched, and X was bound to the value "5". This was reflected in the answer.

The answer was not "no." Therefore, we say that the query succeeded, just as if the answer had been "{}" meaning "yes".

## Real meaning of {}

We see now that "{}" means "the query succeeded, but there were no variable bindings."

## Another example of variable usage

We could also have asked the following question:

`?- student(Y, 3).`

```{Y = Martin}
{Y = John}
```

Now we would get two answers, because two facts in the database matched the goal. In effect, the variable Y would first be bound to the value "Martin", then the binding would be broken, and the variable would be rebound to the value "John". We will get back to how this actually happens later.

## Yet another example of variable usage

If we asked the following question:

`?- student(Martin, S), student(John, S).`

`{S = 3}.`

The answer is computed as follows:

1. Prolog tries to satisfy the first subgoal, "student(Martin, S)". This succeeds, with the variable binding {S = 3}.
2. Next, Prolog tries to satisfy the subgoal "student(John, 3)". This subgoal can also be satisfied, and the query succeeds.

## Free variables and bound variables

Notice how, in the second step, the occurrence of "S" has been substituted with the variable binding already obtained for S. Before the first step, S is a free variable, while in the second step, the variable is a bound variable.

## Variable syntax

In Prolog+CG, a variable is any sequence of letters, underscores, and digits that:

1. starts with a letter or an underscore, and

For example, "X", "_garble", "r2d2", "c3po", and "f77" are all variables. However, "garble" is not a variable because it starts with two letters. Instead, it is an identifier. In addition, "13r9" is not a variable because it does not start with a letter or an underscore.

## Summary

A variable is a placeholder for a piece of data whose value we wish to compute. Variables are terms just like structures and string constants, so you can use a variable anywhere you can use a term. Variables can either be free or bound. A bound variable is a variable that has a value, while a free variable does not yet have a value. Variables have special syntax that you need to learn and learn well.

## Next

Next, we look at structures within structures.

PrevLite: 9.4 Matching
NextLite: 9.7 Summary

Prev: 9.4 Matching
Up: 9 A simple Prolog program
Next: 9.6 Structures within structures (Ad)