Jump to content

TI-BASIC

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Ran4 (talk | contribs) at 10:42, 16 August 2009 (Elaborations: Return statement -> Stop statement. Did someone even read the code?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

TI-BASIC is the unofficial name of a BASIC-like language built into Texas Instruments (TI)'s graphing calculators, including the TI-83 series, TI-84 Plus series, TI-89 series, TI-92 series (including Voyage 200), TI-73, and TI-Nspire. TI does not officially name the language, but only refers to it as "extensive programming capability". Though the TI-BASIC name has stuck, the syntax is different from almost all standard BASIC implementations, sometimes resembling other languages such as PILOT or even Forth.

It is, for many applications, the most convenient way to program any TI calculator, since the capability to write programs in TI-BASIC is built-in. Assembly language (often referred to as "asm") can also be used, and C compilers exist for translation into assembly: TIGCC for Motorola 68000 based calculators, and Z88DK for Zilog Z80 based calculators. However, both of them are cross-compilers, not allowing on-calculator programming. Although TI-BASIC is considerably slower when compared to assembly, (not as useful for programming fast applications and games) it is very useful for writing programs quickly to solve math problems (formulae, etc) and for performing repetitive tasks, with some math instruction books even providing programs in TI-BASIC (usually for the widespread variant used by the TI-82/83/84 series).

Although it is somewhat minimalist compared to programming languages used on computers, TI-BASIC is nonetheless an important factor in the programming community. Because TI graphing calculators are required for advanced mathematics classes in many high schools and universities, TI-BASIC is often the first glimpse many students have into the world of programming. Learning to program in TI-BASIC is a relatively easy task, encouraging potential programmers to explore the field of computer science.

Wikibooks
Wikibooks

Wikibooks has textbooks on TI-BASIC programming:

Syntax

The syntax of all versions of TI-BASIC are somewhat different from typical BASIC implementations. The language itself has some basic structured programming capabilities, but makes limited to no use of or allowance for white space or indentation. It is also dependent on a slightly nonstandard character set, with specific characters for assignment (the right arrow, not readily available in most character sets), square and cube roots, and other mathematical symbols, as well as tokenized entry and storage for keywords. All statements begin with a colon, which also functions as a statement separator within lines.

Expressions use infix notation, with standard operator precedence. Many statements include their arguments in parentheses, similar to the syntax used for mathematical functions. The assignment syntax is unusual; rather than using a let statement or an equals sign, TI-BASIC uses a right-arrow operator with the syntax: source → destination.

Control Flow

Control flow statements include if-then-else blocks, for loops, while loops, and repeat loops, though no switch statements. Unusually for a high level language, TI-BASIC implementations include IS> (Increment and Skip if Greater Than) and DS< (Decrement and Skip if Less Than) statements, constructs generally associated with assembly languages. Sections of programs can be labeled; however, particularly on the Z80 models, the labels function primarily as destinations for GOTO statements rather than as program or block labels.

Availability of functions and subroutines depends on the implementation; the versions available on the TI-82-descended calculators do not even support a GOSUB-like function, though it is possible to call programs from within each other and share variables between programs. TI-89/92-based designs can have access to shared functions, essentially programs capable of returning a value.

Data Types

TI-BASIC is a strongly and dynamically-typed language. Available data types are:

  • Integers, which could store a large amount of data. The 68k calculators could store very large numbers, as high as , with perfect accuracy.
  • Real numbers, using decimal floating point. These store up to 14 significant digits depending on the calculator model.
  • Complex numbers, implemented as pairs of reals.
  • Strings
  • Lists, which are one-dimensional arrays of numbers which support elementwise operations. On the 68k calculators, elements can be integers, reals, complex numbers, or expressions.
  • Matrices, with elements subject to the same restrictions in lists
  • Symbolic expressions, unique to the 68k series.

Data types that cannot be directly manipulated (typing only their name on a line would result in an error) include:

  • Pictures
  • Data, unique to the 68k series
  • Programs
  • Functions, unique to the 68k series

It is not possible to create user-defined data types without using a library written in assembly. Lists are often used as a replacement for structs.

Variables

Flexibility in the use of variables varies widely by the calculator model. On the TI-82, the programmer was limited to 27 real variables (A-Z and Θ), and a limited number of predefined variable names of other types (e.g., lists had to be one of the six variables L1-L6.) All variables were global.

In contrast, the 68k calculators allow all variable names to have up to eight alphanumeric (including Greek) characters. Futhermore, variables can be grouped into "folders", or made local to a program (by declaring them with the Local statement).

Comments

The 68k calculators allow programs to include single-line comments, using © as a comment symbol. If a comment appears as the first line after the "Prgm" statement, it is displayed in the status bar when the program is selected in the catalog; such comments are often used to document the names or types of parameters.

z80 programmers often start lines with " to denote a comment - the line is interpreted as a string, and not executed.

Functions

The 68k version of TI-BASIC allows creating user-defined functions. Functions have the same syntax as programs except that they use the Func...EndFunc keywords instead of Prgm...EndPrgm, and that they are not allowed to use instructions that perform I/O, modify non-local variables, nor call programs. However, functions can still be non-pure because they can call built-in functions such as getTime(), getKey(), or rand().

All functions have a return value, which in the absence of an explicit Return statement is the last expression evaluated.

Examples

These examples are slightly TI-83-series biased. For example, "ClrHome" would be "ClLCD" on the TI-85 and TI-86.

An entirely different command is required to clear the screen in the TI-89. Since output is generally displayed on the ProgramIO screen, the "ClrIO" command is required to clear the output screen. There exists a "ClrHome" command on the TI-89, and it performs its function - namely, clearing the Home screen. For programming purposes, however, this command is essentially useless.

Hello world

The following program, when executed, will display the phrase "HELLO, WORLD!":

Z80 Series

PROGRAM:HELLOWLD
:ClrHome
:Disp "HELLO, WORLD!"

68k Series

hellowld()
:Prgm
:  ClrIO
:  Disp "Hello, World!"
:EndPrgm

Lists and Loops

The significance of lists in terms of programming is quite often overlooked. Needless to say, lists, in combination with loops, are quite useful in creating programs that can handle much more than a set number of inputs. That is:

Lists

Z80 Series

PROGRAM:LISTS
:Input "NUMBER OF VARIABLES ",A
:If A<1 or fPart(A)
:Stop
:For(N,1,A)
:Input "VAR ",B
:B→L1(N)
:End
:L1

68k Series

lists()
:Prgm
:  Local n,i,c
:  {}→list
:  Input "Number of variables?",n
:  For i,1,n
:    Input "Variable #"&string(i),c
:    c→list[i]
:  EndFor
:EndPrgm

In addition, lists are important for saving save game data and highscore data in programs. This is better than storing highscore or save game information as a variable, as variables are commonly changed during calculations performed by the user. Lists on the TI-82 cannot have custom names (L1 through L6 are preprogrammed).

Elaborations

Essentially, this program can be used to compile a list containing an arbitrary number of elements, which could then be implemented in a larger program that would break up the list and put each element to use. For instance, if we were to create a polynomial equation solver, we would use the technique noted above to compile all the coefficients into a list. Under the guidelines of the Rational Root Theorem, we would implement the first and last elements into a program to be factored and paired (and put into another list). To finish, we would create another While loop which would take the list with the factored elements, raise them to the appropriate power (this can be done by finding the "dim(L1)", subtracting one from it, and implementing another While loop which would subtract from the "dim(L1)" with intervals of 1), multiplying the result to the elements of the original list, adding the new results, and checking to see if the sum (of the new result) is equal to 0.

The "If A<1 or fPart(A)" statement checks to see if the variable A is a negative number, is zero or a non-integer. If so, the program is stopped by the Stop statement. If the condition is not true, then the program continues on to the rest of the code. The first variable is used to define L1. In the For( loop, only the commands between For(N,2,A) and the first End are considered. Every time the For( loop is executed, an input is asked for, and the element is added to the end of the list, and saved. By this time, the list L1 should have a dim(L1) = A and be ready for manipulation.

Counting

Z80 Series

PROGRAM:COUNTING
:10^10→B
:For(A,1,B)
:Disp A
:End

68000 Series

counting()
:Prgm
:  Local a,b
:  10^10→b
:  For a,1,b
:    Disp a
:  EndFor
:EndPrgm

Recursion

Recursion is possible, as you can call the same program from within itself or from within another program.

Z80 Series

PROGRAM:FACTUI
:"A user interface for FACT"
:Prompt X
:1→A:"An accumulator of sorts. The answer will be stored here."
:prgmFACT
PROGRAM:FACT
:If X=0
:Then
:Disp A
:Stop
:End
:XA→A
:X-1→X
:prgmFACT

68k Series

factui()
:Prgm
:  Local fact, n
:  Define fact(x)=Func
:    If x=0
:      Return 1
:    Return x*fact(x-1)
:  EndFunc
:  Input "Input a number", n
:  Disp "n! = "
:  Disp fact(n)
:EndPrgm

Functions

The 68k series makes a distinction between programs and functions. Functions are just like programs except that they do not allow statements with side effects (like Input or Disp, nor modifying non-local variables), and they return a value, which in the absence of an explicit Return statement is the last expression evaluated.

fact(x)
:Func
:  If x=0
:    Return 1
:  If x<0
:    Return undef
:  x*fact(x-1)
:EndFunc

See also