Back: 1.1 Simple Statements Forward: 1.1.2 Invoking a procedure     FastBack: 1. Basic Ideas Up: 1.1 Simple Statements FastForward: 2. Using Array Syntax         Top: Yorick: An Interpreted Language Contents: Table of Contents     About: About This Document

1.1.1 Defining a variable

The following five Yorick statements define the five variables c, m, E, theta, and file:

 
c = 3.00e10;  m = 9.11e-28
E = m*c^2
theta = span(0, 6*pi, 200)
file = create("damped.txt")

Variable names are case sensitive, so E is not the same variable as e. A variable name must begin with a letter of the alphabet (either upper or lower case) or with an underscore (_); subsequent characters may additionally include digits.

A semicolon terminates a Yorick statement, so the first line contains two statements. To make Yorick statements easier to type, you don’t need to put a semicolon at the end of most lines. However, if you are composing a Yorick program in a text file (as opposed to typing directly to Yorick itself, a semicolon at the end of every line reduces the chances of a misinterpretation, and makes your program easier to read.

Conversely, a new line need not represent the end of a statement. If the line is incomplete, the statement automatically continues on the following line. Hence, the second and third lines above could have been typed as:

 
E=
  m *
  c^2
theta= span(0, 6*pi,
            200)

In the second line, * and ^ represent multiplication and raising to a power. The other common arithmetic operators are +, -, / (division), and % (remainder or modulo). The rules for forming arithmetic expressions with these operators and parentheses are the same in Yorick as in Fortran or C (but note that ^ does not mean raise to a power in C, and Fortran uses the symbol ** for that operation).

The span function returns 200 equally spaced values beginning with 0 and ending with 6*pi. The variable pi is predefined as 3.14159...

The create function returns an object representing the new file. The variable file specifies where output functions should write their data. Besides numbers like c, m, and E, or arrays of numbers like theta, or files like file, Yorick variables may represent several other sorts of objects, taken up in later chapters.

The = operator is itself a binary operator, which has the side effect of redefining its left operand. It associates from right to left, that is, the rightmost = operation is performed first (all other binary operators except ^ associate from left to right). Hence, several variables may be set to a single value with a single statement:

 
psi = phi = theta = span(0, 6*pi, 200)

When you define a variable, Yorick forgets any previous value and data type:

 
phi = create("junk.txt")

Back: 1.1 Simple Statements Forward: 1.1.2 Invoking a procedure     FastBack: 1. Basic Ideas Up: 1.1 Simple Statements FastForward: 2. Using Array Syntax         Top: Yorick: An Interpreted Language Contents: Table of Contents     About: About This Document