# Declaring Variables

### Declaring Variables in Triggering Events:

Perhaps the most common way is to declare the variable implicitly within the triggering event or context of a rule. The purpose if this is to allow the programmer to extract information from the matched event so that it may be used in the associated plan. For example, if you have a rule whose triggering event is a goal event related to the addition of two integer numbers, you will need to be able to get those two numbers any time the rule is used to handle a goal event. To illustrate this, the snippet of code below is an example of such a rule that generates a belief containing the result:
```rule +!addition(int X, int Y) {
+result(X, Y, X+Y);
}```

In the above rule, we declare two variables X and Y as part of the rules triggering event. this rule stores the result of the addition in a belief with predicate “result”.

NOTE: This example is trivial, and you would simply use the expression X+Y to perform the calculation in practice.

### Declaring Variables in the Rule Context:

A second way of declaring variables is within the context of a plan rule. The purpose of this is to allow the programmer to extract information from the agents beliefs that has been used as part of the rule selection process to identify that the rule is applicable in the current context. For example, we can expand on our simple previous example about addition to develop a second rule (it should be written before the above rule) that attempts to reuse the knowledge of previous calculations which are stored in the result belief:

```rule +!addition(int X, int Y) : result(X, Y, int R) {
// do nothing because we already know the result!
}```

In the above example, the rule states that: if we are adding two integer numbers X and Y, and we already have a belief that contains some result for X and Y, R, then use this rule in preference over the earlier rule because we already know the result of the calculation.

Another example that uses the information extracted from the context is given below:

```rule +!tick() : time(int T) {
C.println("The time is: " + T);
}```

The context of this rule is that we know the current time, and if this is true, we extract the value representing the current time in to a variable T which we then print to the console.

### Declaring Variable in Statement Guards:

A third way of declaring variables is in the guard of some of the statements. For the moment, the only place that we can do this is in the guard of a query statement:

```rule +!tick() {
query(time(int T));
C.println("The time is: " + T);
}```

The above rule is basically the same as the previous example. The difference is that the presence of a belief about the current time is assumed (the agent must always have a belief about the current time). The query simply matches the guard against that belief and binds the variable T to the corresponding value.

As will be seen in the next lesson, variables can also be introduced in while loops and if statements.

### Declaring Variables Explicitly:

The final way of declaring variables is by using an explicit variable declaration statement (in the same way that local variables are normally declared in procedural programming languages). The motivation for this is the same as in any procedural language given the body of a plan rule is simply a procedural block of code. For example, let us consider an alternative to our addition goal that calculates the hypotenuse of a right-angled triangle:

```rule +!hypotenuse(double X, double Y) {
double hypotenuse = math.sqrt(X*X+Y*Y);
+result(X, Y, hypotenuse);
}```

Naturally, it is possible to write the above code in a single line, but if the calculation were more complex, then the above structure may make the code more readable.