BASIC programming language
BASIC (Beginner's All-purpose Symbolic Instruction Code) is a family of high-level programming languages. Originally invented in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College, it was designed to allow students not in science fields to use computers. At the time all computer use required writing custom software, which was something only scientists and mathematicians tended to do. It became widespread on home microcomputers in the 1980s, and remains popular to this day in a handful of heavily evolved dialects.
History
Background
Prior to the mid- 1960s, computers were highly expensive tools used only for special-purpose tasks. In a simple form of batch processing, these machines ran only a single "job" at a time. During the 1960s, however, computer prices started to drop to where even small companies could afford them, and their speed increased to the point where they often sat idle without jobs to run.
Programming languages of the era tended to be designed, like the machines on which they ran, for specific purposes (such as scientific formula processing or text editing). Since single-job machines were expensive, the tendency was to consider execution speed the most important feature of a language. In general, these specialized languages were difficult to use and used widely disparate syntax.
It was at this time that the time-sharing system concept started to become popular. In such a system the processing time of the main computer is "sliced up" and each user is given a small amount in alternation. The machines were fast enough for most users to feel they had a single machine all to themselves. In theory, timesharing reduced the cost of computing tremendously, as a single machine could be shared among hundreds of users.
Early years — the mini computer era
The original BASIC language was invented in 1963 by John Kemeny and Thomas Kurtz and implemented by a team of Dartmouth students under their direction. BASIC was designed to allow students to write programs for the Dartmouth Time-Sharing System. BASIC was intended to address the complexity issues of older languages with a new language designed specifically for the new class of users the time-sharing systems allowed — that is, a "simpler" user who was not as interested in speed as in simply being able to use the machine. In the following years, as other dialects of BASIC appeared, Kemeny and Kurtz' original BASIC dialect became known as Dartmouth BASIC.
The eight design principles of BASIC were:
- Be easy for beginners to use.
- Be a general-purpose programming language.
- Allow advanced features to be added for experts (while keeping the language simple for beginners).
- Be interactive.
- Provide clear and friendly error messages.
- Respond fast for small programs.
- Not require an understanding of computer hardware.
- Shield the user from the operating system.
The language was based partly on FORTRAN II and partly on ALGOL 60, with additions to make it suitable for timesharing and, later, text processing and matrix arithmetic. BASIC was first implemented on the GE-265 mainframe which supported multiple terminals. Contrary to popular belief, it was a compiled language at the time of its introduction.
The designers of the language decided that it should remain in the public domain so that the language would become widespread. They also made it available to high schools in the Dartmouth area and spent a considerable amount of effort in promoting the language. As a result, knowledge of BASIC became relatively widespread for a computer language and BASIC was implemented by a number of manufacturers, and became fairly popular on newer minicomputers like the DEC PDP series and the Data General Nova. In these instances the language tended to be implemented as an interpreter, instead of (or in addition to) a compiler.
Several years after its release, highly-respected computer professionals, notably Edsger W. Dijkstra, expressed their opinions that the use of GOTO statements, which existed in many languages including BASIC, promoted poor programming practices. Some also derided BASIC as too slow or too simple.
Explosive growth — the home computer era
Notwithstanding the language's use on several minicomputers, it was the introduction of the Altair 8800 microcomputer in 1975 that truly spread BASIC. Most programming languages were too large to fit in the small memory most users could afford on these machines, and with the slow storage on paper tape (or later audio cassette tape: disks of any kind were not available at any price for some years) and the lack of suitable text editors, a small language like BASIC was a good fit. BASIC also had the advantage that it was fairly well known to the young designers who took an interest in microcomputers at the time as a result of Kemeny and Kurtz's earlier proselytizing. One of the first to appear for this machine was Tiny BASIC, a simple BASIC implementation originally written by Dr. Li-Chen Wang, and then ported onto the Altair by Dennis Allison at the request of Bob Albrecht (who later founded Dr. Dobb's Journal). The Tiny BASIC design and the full source code were published in 1976 in DDJ.
Newer companies attempted to follow the successes of MITS, IMSAI, North Star and Apple, thus creating the home computer revolution; meanwhile, BASIC became a standard feature of all but a very few home computers. Most came with a BASIC interpreter in ROM. Soon there were many millions of machines running BASIC around the world, likely a far greater number than all the users of all other languages put together.
In 1975, Micro-Soft (then only two people — Bill Gates and Paul Allen) released Altair BASIC. The version written for the Altair was co-authored by Gates, Allen and Monte Davidoff. Versions of Microsoft BASIC then started appearing on other platforms under license, and millions of copies and variants were soon in use; it became one of the standard languages on the Apple II. By 1979, Microsoft was talking with several microcomputer vendors, including IBM, about licensing a BASIC interpreter for their computers. A version was included in the IBM PC ROM chips and PCs without floppy disks automatically booted into BASIC.
There are more dialects of BASIC than there are of any other programming language. All but a very few home computers of the 1980s had a ROM-resident BASIC interpreter.
The BBC published BBC BASIC, developed for them by Acorn Computers Ltd, incorporating many extra structuring keywords, as well as comprehensive and versatile direct access to the operating system. It also featured a fully integrated assembler. BBC BASIC was a very well-regarded dialect, and made the transition from the original BBC Micro computer to more than 30 other platforms.
Maturity — the personal computer era
Many newer BASIC versions were created during this period. Microsoft sold several versions of BASIC for MS-DOS/ PC-DOS including BASICA, GW-BASIC (a BASICA-compatible version that did not need IBM's ROM) and Quick BASIC. Turbo Pascal-publisher Borland published Turbo BASIC 1.0 in 1985 (successor versions are still being marketed by the original author under the name PowerBASIC).
These languages introduced many extensions over home computer BASIC, such as improved string manipulation and graphics support, access to the file system and additional data types. More important were the facilities for structured programming, including additional control structures and proper subroutines supporting local variables.
However, by the latter half of the 1980s newer computers were far more complex. At the same time, computers had progressed from a hobbyist interest to tools used primarily for applications written by others, and programming as a whole became less important for the growing majority of users. BASIC started to fade, though numerous versions remained available.
BASIC's fortune reversed once again with the introduction of Visual Basic from Microsoft. Though it is somewhat difficult to consider this language to be BASIC, because of the significant shift in paradigm towards an object-oriented and event-driven language. While this could be considered an evolution of the language, few of the features of Dartmouth BASIC, such as line numbers and the INPUT
keyword, remain.
Many BASIC dialects have also sprung up in the last few years, including Bywater BASIC and True BASIC. Many other BASIC variants and adaptations are authored by hobbyists, equipment developers, and others, as it is relatively easy to develop interpreters and compilers for BASIC.
The language
Syntax
Basic statements are terminated by line endings unless there is a line continuation character. A very minimal BASIC syntax only needs the LET, PRINT, IF and GOTO commands.
Line numbers were a very distinctive aspect of classic home computer BASIC. Some BASIC interpreters later appeared with a built-in RENUMBER command.
Some (but not all) modern BASIC dialects have abandoned line numbers, and support most (or all) of the structured control and data declaration constructs known in other languages like C and Pascal (note also that some advanced versions of line number-based home computer BASICs incorporated such constructs as these to good effect):
- do - loop - while - until - exit
- on x goto / gosub (switch & case)
Recent variants such as Visual Basic have introduced object-oriented features, such as the For Each...Loop construct for looping through collections and arrays in VBA and Visual Basic 4 and later, and even inheritance in the latest version. Memory management is easier than in many other procedural programming languages because of the commonly included garbage collector (presumably for which, however, one pays a run-time performance penalty).
This wealth of variants shows that the language is an "organic" one and that it may be seen as a subculture dealing with computer programming rather than as a fixed set of syntactic rules. This applies as well to other "old" computer languages like COBOL and FORTRAN, although the BASIC movement is by far the largest; this may be explained by the large number of IT professionals who learned to program in BASIC during the home computer era in the 1980s.
Procedures and flow control
BASIC does not have a standard external library like other languages such as C. Instead, the interpreter (or compiler) contains an extensive built-in library of intrinsic procedures. These procedures include most of the tools a programmer needs to learn programming and write simple applications, including functions for math, strings, console input/output, graphics and file manipulation.
Some BASIC dialects do not allow programmers to write their own procedures. Programmers must instead write their programs with large numbers of GOTO statements for branching. This can result in very confusing source, commonly referred to as spaghetti code. GOSUB statements branch to simple kinds of subroutines without (sometimes with) parameters or local variables. Most modern versions of BASIC such as Microsoft QuickBASIC have added support for full subroutines and functions. This is another area where BASIC differs from many other programming languages. BASIC, like Pascal, makes a distinction between a procedure which does not return a value (called a subroutine) and a procedure which does (called a function). Many other languages (notably C) make no distinction and consider everything a function (with some returning a "void" value).
While functions in the larger sense of subroutines returning values were a latecomer to BASIC dialects, many early systems supported the definition of one-line mathematical functions by DEF FN ("DEFine FunctioN"). The original Dartmouth BASIC also supported Algol-like functions and subroutines from an early date.
Data types
BASIC is well known for good string manipulation functions. Early dialects already had a set of fundamental functions (LEFT$, MID$, RIGHT$) to deal with strings easily. Because strings are often used in everyday applications this was a considerable advantage over other languages at the time of its introduction.
The original Dartmouth BASIC supported only numeric and string data types. There was no integer type. All numeric variables were floating point. Strings were dynamic in length. Arrays of both numbers and strings were supported, as well as matrices (two dimensional arrays).
Every modern BASIC dialect at least has the integer and string data types. Data types are usually distinguished by a suffixed character; string identifiers end in $, whereas integers classically ended with a '%'. In some dialects, variables must be declared (with DIM) on their first usage; other dialects do not require it, but can optionally enforce it—typically using a directive such as Option Explicit (in VB.NET it is on by default but can be turned off using Option Explicit Off). Many dialects also support such additional types as 16- and 32-bit integers and floating-point numbers. Some have "polynomial", "complex", "list", and specialized types for the intended use of the implementation. Additionally, some allow user-defined types similar to Pascal " records" or C "structs".
Most BASIC dialects beyond the most primitive also support arrays of integers or other types. In some, arrays must be pre-allocated (with the DIM statement) before they can be used. Support for two- and higher-dimensional arrays, as well as arrays of non-integer types, is common.
Visual Basic .NET DIM myIntArray (100) AS INTEGER DIM myStringArray (50) AS STRING
Depending on the dialect of BASIC, the resulting available elements in the array can vary. In some, commands similar to the first in the example will define a 101-element array of integers with elements from 0-100, others may define a 100-element array of integers with elements 1-100 or, rarely, 0-99. It may also be possible to set a default beginning element number other than 0 or 1 with an additional command such as Option Base. The Visual Basic .NET example will create an array from 0-100.
When working with strings, commands similar to the second may allocate a 50-element array of variable-length strings, or may allocate a single string of 50 characters. Note that they are also subject to the numerical differences mentioned above.
These non-standard and, to some, less than intuitive dimensioning can often trip up the novice (and even the advanced) programmer. Newer dialects offer specific dimensioning, such as DIM myIntArray (10 TO 20) AS INTEGER which would be an 11-element array from 10-20.
Relational, logical operators
= equal <= less than or equal NOT logical negation <> not equal >= greater than or equal AND logical conjunction < less than OR logical disjunction > greater than
Note that there is no lexical distinction between the assignment operator and the equality operator in BASIC; a single equal sign is used for both. There is, however, a method available to the programmer if a visible difference between the two is wanted: the optional LET keyword allows for assignments to be clearly and unambiguously distinguished from the use of the equality operator. Example: IF X=7 THEN LET Y=3
.
Examples
Hello, world!
The archetypal Hello world program might look like this when written in "classical" BASIC:
10 PRINT "Hello, world!" 20 END
Classic BASIC
Classic BASIC. Note that this example is actually well structured, demonstrating that use of the GOTO statement does not necessarily lead to an unstructured program.
10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 30 REM 40 INPUT "How many stars do you want: "; N 50 S$ = "" 60 FOR I = 1 TO N 70 S$ = S$ + "*" 80 NEXT I 90 PRINT S$ 100 REM 110 INPUT "Do you want more stars? "; A$ 120 IF LEN(A$) = 0 THEN GOTO 110 130 A$ = LEFT$(A$, 1) 140 IF (A$ = "Y") OR (A$ = "y") THEN GOTO 40 150 PRINT "Goodbye "; 160 FOR I = 1 TO 200 170 PRINT U$; " "; 180 NEXT I 190 PRINT
Modern BASIC
"Modern" structured BASIC (for example, QuickBASIC and PowerBASIC), replacing GOTO statements by more modern keywords.
INPUT "What is your name"; UserName$ PRINT "Hello "; UserName$ DO INPUT "How many stars do you want"; NumStars Stars$ = "" Stars$ = REPEAT$("*", NumStars) ' <- ANSI BASIC --or-- Stars$ = STRING$(NumStars, "*") ' <- MS BASIC PRINT Stars$ DO INPUT "Do you want more stars"; Answer$ LOOP UNTIL Answer$ <> "" Answer$ = LEFT$(Answer$, 1) LOOP WHILE UCASE$(Answer$) = "Y" PRINT "Goodbye "; FOR I = 1 TO 200 PRINT UserName$; " "; NEXT I PRINT