42

When you write something in BASIC, you are required to use line numbers. Like:

10 PRINT "HOME"
20 PRINT "SWEET"
30 GOTO 10

But I wonder: who came up with the idea to use line numbers at all? It is such a nuisance, and left quite an "echo" in the developing (pun intended) world!

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69

16 Answers16

78

The idea back then was that you could easily add code everywhere in your program by using the appropriate line number. That's why everybody uses line numbers 10, 20, 30.. so there is room left:

10 PRINT "HOME"
20 PRINT "SWEET"
30 GOTO 10
25 PRINT "HOME"

On the first interfaces BASIC was available for, there was no shiny editor, not even something like vi or emacs (or DOS edit, heh). You could only print out your program on the console and then you would add new lines or replace them, by giving the appropriate line number first. You could not navigate through the "file" (the program was kept in memory, although you could save a copy on disk) with the cursor like you are used to nowadays.

Therefore the line numbers weren't only needed as labels for the infamous GOTO, but indeed needed to tell the interpreter at what position in the program flow you are editing.

ypnos
  • 50,202
  • 14
  • 95
  • 141
46

It has a loong-loong history.

Line numbering actually comes from Dartmouth BASIC, which was the original version of the BASIC programming language and was the integral part of a so called Dartmouth Time Sharing System. That DTSS had a rudimentary IDE, which was nothing more than an interactive command line.

So every line typed inside this "IDE", and beginning with a line number, was added to the program, replacing any previously stored line with the same number; anything else was assumed to be a DTSS command and immediately executed.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • 5
    +1 - *this* is the right answer. The combination of line editor and immediate execution in the same environment was probably BASIC's most important contribution. – Jules Feb 10 '16 at 22:22
43

Before there was such a thing as a VDT (video display terminal), we old-timers programmed on punch cards. Punch cards reserved columns 72-80 for sequence numbers - if you dropped your card deck and they all got out of order, you could put the deck in a card sorter that would order the cards based on those sequence numbers. In many ways, the BASIC line numbers were similar to those sequence numbers.

Another advantage in the BASIC world is that in the old days BASIC was interpreted as it was run. Using labels rather than sequential line numbers for branches would require a first pass to pick up all the labels and their locations, where as if you use line numbers the interpreter knows whether it needs to start scanning forwards or backwards for the destination.

Community
  • 1
  • 1
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 3
    This is the only right answer! – cwallenpoole Feb 12 '09 at 14:54
  • Sort of a combination of the end-of-the-card sequence numbers and the beginning-of-the-card line labels. Easy to see how it looked like a win. – David Thornley Feb 12 '09 at 15:11
  • @U62 - I was explaining where the concept of sequence numbers came from, not where BASIC came from. – Paul Tomblin Feb 12 '09 at 15:19
  • Even though it's irrelevant? GJ – U62 Feb 12 '09 at 17:49
  • 3
    It's not irrelevant, it's history. Do you consider your parents irrelevant because they're not you? – Paul Tomblin Feb 12 '09 at 18:17
  • @U62 - The answer is still relevant because the convention of using sequence numbers was carried forward. It's ridiculous to suppose that programming conventions still common in the 70s had no influence on what was adopted in the 80s. Progress takes time. – Parappa Mar 13 '10 at 02:29
  • 3
    BASIC was around in the 1970's... and there were punchcard fed basic implementations. As a kid, I earned my D&D buying money punching cards for CS majors, including some BASIC. – aramis Jun 30 '12 at 20:30
  • 2
    Yep I agree. It definately was a tie over from the mentality of punch cards. Also since most early home computers had a line numbered basic built in (Apple, Commodore, etc) it became very common. Interestingly enough, this also allowed you to enter your programs non-sequentially, which helped with their limited editting capabilites. One of the first things I wrote in BASIC was a text edit program. But as soon as I got my hand on a Vax with it's Extended BASIC, I was so happy to never see a line number again. – Swanny Jan 17 '13 at 03:16
  • 1
    Clearly the punch card comparison is wrong, because by the time BASIC was invented, line numbering in, eg, Fortran was *optional*, and BASIC changed that to make it compulsory, which is what the question is asking about. – Jules Feb 10 '16 at 22:18
  • 4
    Also, BASIC was originally implemented as a compiler, not an interpreter (similarly to the way many LISP implementations compile each expression on entry prior to execution), so interpreter internals are irrelevant also. – Jules Feb 10 '16 at 22:20
20

Back in the day you didn't have a 2 dimensional editor like emacs or vi. All you had was the command line.

Your program was stored in memory and you would type in single line commands to edit single lines.

If you were a Unix god you could do it with ed or something, but for BASIC on a C-64, VIC-20, or TRS-80 you'd just overwrite the line.

So a session might look like:

$10 PRINT "Hellow World"
$20 GOTO 10
$10 PRINT "Hello World"

And now the program would work correctly.

Some older mainframes even had line terminals without a screen. Your whole session was printed on paper in ink!

Sean Cavanagh
  • 4,727
  • 2
  • 19
  • 14
9

The "Who?" would be the inventors, Kemeney and Kurtz.

After reading the replies, I checked the Wikipedia entry for "Dartmouth BASIC", and was surprised to learn

The first compiler was produced before the time-sharing system was ready. Known as CardBASIC, it was intended for the standard card-reader based batch processing system.

So, it looks like Paul Tomblin "gets the square".

gbarry
  • 10,352
  • 6
  • 33
  • 43
  • And in high school, we had a "modem" that let us dial into Dartmouth's computer, and experience this wonder first hand. – gbarry Mar 07 '09 at 18:58
  • This was a prototype, implemented as an experiment while the teletype-based interface that the language was being designed for was still being developed. While I have no direct evidence either way, I'd bet that either (a) this first version didn't have compulsory line numbering (like Fortran) or (b) the use of line numbers to provide program editing versus immediate execution had already been planned. – Jules Feb 10 '16 at 22:27
8

Paul Tomblin's answer is the most comprehensive, but I'm surprised no one has mentioned that a big part of the BASIC project's initial goal was to provide a beginner-friendly interactive environment using timesharing. (Kurtz and Kemeny's vision for "universal access for all students" was far ahead of its time in this regard.)

The BASIC system that was developed to fulfill this goal featured Teletype ASR-33 (and later other) printing terminals. When connected to a timesharing-capable OS, these allowed editing and running BASIC programs in an interactive mode (unlike working with punched cards), but they are not cursor-addressable. Line numbers were a beginner-friendly way to both specify the order of program statements and allow unambiguous editing in the absence of a screen editor. The Wikipedia entry for "line editor" explains further, and anyone who's ever tried to use a line editor (such as the Un*x 'ed') can appreciate why Kurtz and Kemeny should be thanked for sparing the beginner having to learn the cryptic command sequences required for editing text in this manner.

Armando Fox
  • 310
  • 3
  • 12
  • And right about now (early 2014) is the 50th anniversary of BASIC! – Phil Perry May 07 '14 at 13:44
  • The deepest line editor problem BASIC's numbers solved is figuring what's the current program without re-printing it (slow & wastes paper). So you tend to accumulate some edit commands until you have to `LIST` again, but in most editors like `ed` each insertion/deletion shifts line numbers, so if you see `17i` inserted 2 lines, followed by `24d`, your brain has to track that line 24 at that point = line 22 in last listing . BASIC non-consecutive numbers remain _stable_, making it way easier to follow the code evolution. – Beni Cherniavsky-Paskin Jun 30 '23 at 15:59
  • Hardcopy was a curse and a blessing: You get to keep the whole transcript ! When mixing debugging actions with code changes, you essentially get a "lab notebook" of all experiments you did, from which you want to deduce the bug ️ So an easy ability to track what was the source code at each point = poor man's Git = better debug powers. To get a feeling, [watch Daniel Shiffman on Apple II](https://www.youtube.com/watch?v=7r83N3c2kPw) and note how seeing the numbered edits (1) lets you figure the code easily enough (2) actually _helps_ follow his progress/debugging. Now imagine – Beni Cherniavsky-Paskin Jun 30 '23 at 16:14
6

They originated in FORTRAN, from which BASIC was derived. However, in FORTRAN only lines referenced by other lines (like GOTO targets) needed numbers. In BASIC they had a secondary use, which was to allow editing of specific lines.

starblue
  • 55,348
  • 14
  • 97
  • 151
2

I'd guess it comes from assembler, where each instruction has an address which may be jumped to by another instruction.

Additionally, the first computers didn't have much memory, and storing a line number only takes two bytes (if done properly). Writing a label takes more memory, first in the location, where that label is defined, then in any jump command.

Finally in the good old days there weren't any fancy editors. The only "editor" was a simple command line interface, which treated everything starting with a number being part of a program and everything else as commands to be executed immediately. Most prominent example should be the Commodore 64.

Newer dialects of Basic don't have the need for line numbers any longer.

Kat Cox
  • 3,469
  • 2
  • 17
  • 29
Bodo Thiesen
  • 2,476
  • 18
  • 32
  • The other advantage of sequential line numbers is that you don't have to do a first pass to pick up the labels and their locations - you can just interpret the program and run it in the same pass. – Paul Tomblin Feb 12 '09 at 14:32
2

Back in the fifties, when high programming languages were in their early beginnings, there were no terminals, no editors, no monitors (yes, no monitors), just card punchers and readers (for writing and reading the contents of cards into memory of a computer) and printers (for printing results, naturally). Later, tape was introduced, but that's another story.

Each punch card had its own number. There were several reasons for that; from purely keeping them in order, to determining the sequence of execution. Each card was one line of code (in today's terms). Since, at that time, there were no constructs like if..then..else, or whatever variant of the like, the sequence of execution had to be determined somehow. So GOTO statements were introduced. They were the basis of loops. The term "spaghetti code" comes from that time period also, since badly written code was relatively hard to follow, like spaghetti on a plate :)

Rook
  • 60,248
  • 49
  • 165
  • 242
1

In the early days, most programs were entered with punch cards. The punch cards were usually entered in sequence, usually one instruction per card, with labels (JMP/JSR targets) being a separate instruction card.

To edit your program, you replaced the card.

Later implementations added an optional sequence number on the right end of the line, so that when/if they got out of order, they could be resequenced by an automated reader.

Fortran used both numeric target labels on the left (col 1-5) and left a reserved block on the right (73-80) for sequence or comment.

When BASIC was initially written, it was decided to move the sequence numbers to the left, into FORTRAN's label field, and to allow overwriting prior cards' memory footprint... as an editing mode. This was intended for the interactive dev environment, but worked just as well with cards. And cards were used in some early implementations for a variety of reasons.

Keep in mind: Many computers were card-reader and printer interface right through the late 1970's. Even tho' interactive mode basics were available, card punched basic programs were frequently used. Since many simply were feeding into the IDE, they worked exactly the same way. Including needing a "Run" card at the end. In such cases, one could simply tack a correction card and another Run card to rerun with a variation on some variable; likewise, in complex programs, simply adding a corrected line of a card before the run was adequate to edit out problems without spending precious time finding the errant card itself.

aramis
  • 123
  • 5
1

in Basic, if you didn't have a line number, how can you preform a

GOTO 10 

that was a way to jump lines, a good way that was found ... more than 20 years ago!

today, the lines help us catching errors/exceptions because the debug engines are made to send us in the message that we got an exception on line xxx and we jump right away to it!

imagine a world without line numbers... how can a reporter be paid without the lines?

"Now that you know the novel, you have to write a summary with no more than 50 lines"

remember this? Even at the school we learn about line numbers!

if it wasn't invented, someone will already invented again so we could use it nicely :)

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • 1
    labels are not the reason for basic line numbers, they are only a side effect such to say. – ypnos Feb 12 '09 at 14:24
  • @ypnos, you've got that backwards. Numbers as labels predated BASIC - in FORTRAN, for example, but in BASIC they took on the editing/insertion function. – Paul Tomblin Feb 12 '09 at 14:26
  • In FORTRAN you do not write the line number before every line of the program. That's what we are talking about here :) – ypnos Feb 12 '09 at 14:41
1

Not all versions of BASIC required line numbers. QBasic, for instance, supported labels. You could then jump to those with GOTO (ignoring Dijkstra's "Go To Statement Considered Harmful," for the moment).

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • QBasic was one of the first versions of BASIC not to require line numbers. It came with a program to remove unneeded line numbers from GW-BASIC code. – dan04 Mar 13 '10 at 05:30
1

The answer is already above. Paul Tomblin wrote it (with a caveat to zabzonk).

Actually, I would argue that any answer which does not mention "punch cards" is incomplete, if it mentions neither punch cards nor FORTRAN, it is wrong. I can say that this is definitively right because my parents both used punch cards on a regular basis (they started with FORTRAN 66 and 77), then migrated to Basic and COBOL in the 80's.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

I like the robot church on Futurama, on the walls were written stuff like

10 SIN
20 GOTO HELL

On the Speccy you couldn't edit a line without the line number.

Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0

I find them very helpful when pairing. I don't have to point at a line when my pair has the keyboard, I can just say, "on line 74, shouldn't that really be getMoreBeer()?"

Don Branson
  • 13,631
  • 10
  • 59
  • 101
  • I think the question was about languages where you had to type in the line numbers yourself. I agree line numbers that are displayed automatically by the editor are very useful . – Mendelt Feb 12 '09 at 14:37
  • yes, you're right. It seemed that there was perhaps some sentiment towards getting rid of them, but some still find them useful, so I thought I'd mention that. – Don Branson Feb 12 '09 at 14:50
-1

The original editor for DOS was a wonderful utility called edlin. You could only edit a single line. To make life even more interesting in many versions of BASIC you could type lines out of order, line 10, 20, 30, 25, 5, The execution would be by line line number not by the order of appearance.

Jim C
  • 4,981
  • 21
  • 25