NeturalMath Quick Start Guide

Part 4: Using Variables to Perform Operations

This document is not complete, and my represent features which are not yet implemented in the current version of NeturalMath.

Although the arithmetic functionality of NeturalMath is impressive, it still would not be much more than a glorified calculator if it did not allow the user to define more complex mathematical relationships. It is these capabilities of NeturalMath that allow it to perform advanced forms of math such as algebra and calculus much in the same way that humans do. These mathematical constructs are known as symbols. By manipulating these symbols, it is possible to perform almost any mathematical calculation with NeturalMath. The simplest type of symbol is known as a variable.

Variables are easy to define and work with. For the most part, they behave exactly as one would expect a variable to behave in conventional mathematics--it represents either a placeholder for an unknown value, or a "stand in" for a known value. Please consider the example below:
x = 3
x + 12

Explicit and Implicit Variables

The variable x is defined as having a value of three. When that value is added to the number twelve, the expected result is produced. Variables may either be declared explicitly, or implicitly. Explicitly declared variables are those which are defined by setting them to a value using the assignment operation (=). This value may be a constant, another variable, or a complex sequence of operations. Regardless of the case, the variable will hold the value assigned to it until it has been changed. The examples below illustrate some ways to explicitly declare a variable.
x = 12
y = x + 3
z = x * y

Printing Output

You may note that when typing in the examples above into MathConsole, it does not return a value. In the previous examples in this guide, every equation entered into a line caused the console to return a value, so why does it not return a value when we set a variable? This has to do with the way that NeturalMath evaluates each line of input. In cases where the line of input is a computation which returns a definite result, that result is output to the console screen. Because we assigned that value to a variable, that resulting value is put inside the variable rather than displayed on screen. In order to "peek" into the variable and see its value, we can use the print command to output the result of the value on screen, as shown below.
// using the example above
print x
print y
print z

Once a variable has been declared, it is as simple to redefine its value as it is to declare the variable to begin with. Simply use the assignment operation to change its value.
x = 10
print x
print x + 5
x = 5
print x
print x + 5

Using Implicit Variables

As stated earlier, variables may be explicit or implicit. So far, we have only worked with explicit variables, but it is possible to define a variable without setting its value at the same time. This is known as an implicit variable declaration. Consider the example below:
y = x + 2

The variable y is being set to x+2, but x has not been declared! How is this possible? NeturalMath understands the concept of algebraic expressions much in the way that human mathematicians and scientists do. That means that a variable with an unknown value is allowed to exist in an equation alongside other, known values. This comes with certain limitations, however. If the value in a expression is unknown or otherwise undefined, then the value of the overall expression cannot be determined. What this means is that the print command will fail to return any results, and will return an error message instead. This is demonstrated in the example below:
y = x * 3
print y

This does not mean that the variable y is totally useless, however. This variable may be used in further calculations without issue, as long as x is defined before the value is output. The example below shows this:
y = 5 + x
print y // throws an error

x = 3
print y // works ok

Working variables and understanding how algebraic expressions are used in NeturalMath is covered in greater detail in the next topic.

Limitations on Variables

Variable have a few constraints which it is important to familiarize yourself with. First, a variable's name must contain only numbers and letters, and must start with a letter. This may be upper or lower case. It is recommended that you use names which describe how the variable is to be used, or what piece of data it represents to make your programs more readable.
// valid variable names
a = 1
aaa = 1
Aaa = 1
aAa = 1
a1b2 = 1

// invalid variable names
2a = 1
a@b = 1
_a = 1

Variable names are case-sensitive. This is very important. A variable named a and a variable named A are two totally separate entities. They will not have the same value, or even the same kind of data. They are just as unrelated as the variables x and y. Variables may also not be named after an existing variable, function or system keyword. There are also certain other words which may not be used as variable names because they may be used in future releases, or else the designers of the system felt that they were inappropriate to use in the system. For a complete listing of these terms, please see the Reserved Names page.

More information about variables and expressions will be provided in the next tutorial: Part 5: Understanding Expressions

Last edited Nov 27, 2010 at 4:50 PM by zanethorn, version 7

Comments

No comments yet.