5.2. Using Variables

Syntax:

VariableName
Example:
genius> e
= 2.71828182846

To evaluate a variable by itself, just enter the name of the variable. This will return the value of the variable. You can use a variable anywhere you would normally use a number or string. In addition, variables are necessary when defining functions that take arguments (see Section 5.3.1).

TipUsing Tab completion
 

You can use Tab completion to get Genius to complete variable names for you. Try typing the first few letters of the name and pressing Tab.

ImportantVariable names are case sensitive
 

The names of variables are case sensitive. That means that variables named hello, HELLO and Hello are all different variables.

5.2.1. Setting Variables

Syntax:

<identifier> = <value>
<identifier> := <value>
Example:
x = 3
x := 3

To assign to a variable, use the = or := operators. These operators set the value of the variable and return the number you set, so you can do things like

a = b = 5

The = and := operators can both be used to set variables. The difference between them is that the := operator always acts as an assignment operator, whereas the = operator may be interpreted as testing for equality when used in a context where a Boolean expression is expected.

5.2.2. Global variables and scope of variables

Like most programming languages, GEL has two different types of variables: local and global. A local variable only exists in the context of the function where it is set and all functions called from inside this function. A global variable exists in all contexts. When you set a variable on the toplevel command line, outside of any function, then it will be a global. All functions will see it.

When you set a variable inside a function, it will only be seen inside the function, including all function calls that are made inside this function. This is where GEL differs from a language such as C. One could describe local variables as being semi global in this sense. For example the following code will print out 5. but the function g() cannot be called on the top level as a will not be defined.

function f() = (a:=5; g());
function g() = print(a);
f();

Suppose you assign a value to an identifier inside a function, and this identifier is also used for a global variable, or a variable set in a calling function. Then this has the effect of creating a new local variable inside the function. For example the following code will print out 6 and not 5.

a=6;
function f() = (a:=5);
print(a);

Sometimes however it is neccessary to set a global variable from inside a function. For this, use the set function. Passing a string or a quoted identifier to this function sets the variable globally. For example, to set a to the value 3 you could call:

set(`a,3)
or:
set("a",3)

The set function always sets the toplevel global. There is no way to set a local variable in some function from a subroutine. For this you must use passing by reference.

5.2.3. Built-in variables

GEL has a number of built-in ‘variables’, such as e, pi or GoldenRatio. These are widely used constants with a preset value, and they cannot be assigned new values. There are a number of other built-in variables. See Section 10.4 for a full list.

5.2.4. Previous result variable

The Ans and ans variables can be used to get the result of the last expression. For example, if you had performed some calculation, to add 389 to the result you could do:

Ans+389