20

Possible Duplicate:
Why did we bother with line numbers at all?

I'm curious about why early versions of the BASIC programming language had line numbering like in:

42 PRINT "Hello world!"

The text editors back then had no line numbering?

EDIT: Yes, I know they are used for GOTOs, but why? I mean having labels was too computationally expensive?

Community
  • 1
  • 1
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
  • 13
    When I loaded the page, there were no answers. When I finished typing mine up, there were six, including mine. Answer overflow. – Mike Daniels Mar 12 '10 at 20:04
  • 3
    I think you may have hit the nostalgia button here... Lots of *fond* memories from days gone by... – NealB Mar 12 '10 at 20:12
  • 1
    Duplicate: http://stackoverflow.com/questions/541421/why-did-we-bother-with-line-numbers-at-all – gnovice Mar 12 '10 at 20:15
  • 1
    You can enable line numbers in Visual Studio if you wish. It's a good way to reference code when doing a code review (e.g. You can say "Please refactor file xys.cs line numbers 150-160 by breaking out the code into several methods). – Keith Adler Mar 12 '10 at 20:16
  • @NealB: Oh yeah. Almost felt goosebumps when typing my answer :) – Philippe Leybaert Mar 12 '10 at 20:16
  • 4
    @Nissan: those aren't line numbers in the same sense. They are not part of the program, just part of the editor display, like the left margin is. – John Saunders Mar 12 '10 at 20:16
  • Why ask us? Ask Dr. Kurtz -- one of the inventors of BASIC. http://en.wikipedia.org/wiki/Thomas_Eugene_Kurtz – S.Lott Mar 12 '10 at 20:31
  • @John I agree, but they provide a point of reference in much the same way that line numbers did back in the day. – Keith Adler Mar 12 '10 at 21:24

15 Answers15

29

Many microcomputers had a BASIC interpreter in ROM that would start upon bootup. The problem was that there was no text editor or file system to speak of. You had an interactive prompt to do everything through. If you wanted to insert a line of code, you just typed it, starting with the line number. It would insert it into the correct spot in you code. e.g:

>10 print "hello"
>30 goto 10
>20 print "world"
>list
10 PRINT "hello"
20 PRINT "world"
30 GOTO 10
>

(In that example > is the BASIC prompt)

If you wanted to erase a line, you would type something like ERASE 20. Some really fancy systems gave you a line editor (i.e. EDIT 10) And if you didn't plan your line numbers and ran out (how do I insert a line between 10 and 11?) some systems gave you a RENUM command which would renumber your code (and adjust GOTOs and GOSUBs appropriately).

Fun Times!

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • 8
    Not only that but there was no internet in those days. You wanted to access a computer you _walked_ to where it was. Uphill. Both ways. And when Ferruccio says no filesystem, that's _no_ as in no permanent storage at all on the earliest microcomputer systems. Turn it off, everything's gone. Eventually we got the capability to save programs on cassette tape. Sometimes you could even reload the program from the tape, if you managed to get the level right. – Hugh Brackett Mar 12 '10 at 22:13
  • Unfortunately, this answer, even though it is the most popular, is as incorrect as the other 14 answers. The reason for line numbers in BASIC was that at Dartmouth College, where it was created, it was designed to work with teleprinter terminals on a time-sharing computer system. The only way of keeping lines of code in order and accessing them to modify them was with line numbers. People who have never used a teleprinter computer terminal, such as a Teletype Model 33 KSR or Teletype Model 35 ASR simply have no experience from which to provide the correct answer. – Andrew P. Mar 27 '19 at 03:40
  • @AndrewP. - I see your point about needing line numbers to keep track of code on a TTY but that did not require line numbers to be an integral part of the language. I did a bit of FORTRAN and Pascal programming on TTYs. The text editors of the time would print source code with line numbers added to it so that you could refer to them when making edits but they weren't actually part of the source code. I suspect early microcomputers simply stuck with the TTY input model because of the resource constraints of these machines. – Ferruccio Mar 27 '19 at 11:13
9

The original BASIC line numbering was actually an integral part of the language, and used for control flow.

The GOTO and GOSUB commands would take the line, and use it for control flow. This was common then (even though it's discouraged now).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 4
    While your answer is not wrong, a more complete answer would indicate that the lack of interactive text editors made line numbering a necessary evil; so that one could insert additional lines of code between previously entered lines. –  Mar 12 '10 at 20:10
  • @roygbiv: that was indeed the most important reason for the existence of line numbers in BASIC – Philippe Leybaert Mar 12 '10 at 20:11
  • @Philippe: "an important reason", not the most important. Programs were not interesting without GOTO or GOSUB. – John Saunders Mar 12 '10 at 20:13
  • 2
    Don't forget that line numbers were useful for debugging because the compiler/runtime would report back the line number in question when a syntax error/exception occurred. – Keith Adler Mar 12 '10 at 20:13
  • There were many reasons for line numbering, mainly historic. The interactive editing was a useful reason, but not the only one.... – Reed Copsey Mar 12 '10 at 20:15
  • @roygbiv: That is a specific interpreter's implementation detail - there were BASIC flavors that actually read from files instead of being interactive, even in very early versions, and they still required line numbers. – Reed Copsey Mar 12 '10 at 20:16
  • So far in this thread there are two reasons: GOTO/GOSUB and editing... `what are some other reasons?` –  Mar 12 '10 at 20:18
  • @John: you're right. Wrong wording. There were 2 main reasons: line ordering and labeling for GOTO and GOSUB – Philippe Leybaert Mar 12 '10 at 20:19
8

They were used as labels for GOTO and GOSUB

Like this:

10 PRINT "HELLO WORLD"
20 GOTO 10

There were no named labels in some early BASIC versions

They were also required if you wanted to insert a line between 2 existing lines of code, because in the early days, you had no full text editors. Everything had to be typed in the "interactive" interpreter.

So if you typed:

15 PRINT "AND THE UNIVERSE"

The program would become:

10 PRINT "HELLO WORLD"
15 PRINT "AND THE UNIVERSE"
20 GOTO 10

When you ran out of line numbers, you could run a "renumbering" tool to renumber all lines in your program, but in the very early days of the Commodore 64 and other home computers, we didn't even have that, so you'd have to renumber manually. That's why you had to leave gaps of 10 or more in the line numbers, so you could easily add lines in between.

If you want to try out the Commodore 64 interpreter, check out this C64 emulator written in Flash: http://codeazur.com.br/stuff/fc64_final/ (no install required)

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • ("So if you typed") Can you name the text editor you are referring to please? – Giovanni Funchal Mar 12 '10 at 20:07
  • 1
    There was no text editor. Just a command line where you would type your code – Philippe Leybaert Mar 12 '10 at 20:08
  • Yes but is there any name for this command line? I can't find any reference of it on the web, no screenshot, nothing. – Giovanni Funchal Mar 12 '10 at 20:09
  • @Helltone: BASIC was an interpreted language. You typed into the interpreter. – John Saunders Mar 12 '10 at 20:10
  • @JohnFx: What "edlin" are you talking about? What platform was that? – John Saunders Mar 12 '10 at 20:12
  • @Helltone: try one of the many Commodore 64 emulators. It's fun to live in the past for a few minutes :-) There's even a FLASH version of an emulator: http://codeazur.com.br/stuff/fc64_final/ – Philippe Leybaert Mar 12 '10 at 20:13
  • @John edlin was a line editor that came with the first version of MS-DOS - it had nothing to do with BASIC. –  Mar 12 '10 at 20:19
  • @Neil: I'm so confused. So why are we talking about edlin in conjunction with BASIC, which predates MS-DOS by over a decade? – John Saunders Mar 12 '10 at 20:32
  • @John "edlin" was a specific (microsoft) product - a "line editor" is a generic idea for how to edit text. –  Mar 12 '10 at 20:34
  • edlin was a line oriented editor for editing files, but you had to select which line to edit and then edit it. Early BASIC interpreters (WAY before edlin came about) used the screen buffer as a pseudo-history buffer. You could edit a single line by moving the cursor to the line you wanted to edit, make some changes and press enter. When you were good at it, it was pretty powerful :) Those were the days... – Philippe Leybaert Mar 12 '10 at 20:36
  • 1
    @John and actually "edlin" is still available in Windows, at least as of Win2K, which I'm running at this moment. Open a command line prompt and type "edlin foo.txt" to get a taste of its fantastic user interface. –  Mar 12 '10 at 20:36
  • @Neil: I've used edlin to write C programs for the IBM PC XT. – John Saunders Mar 12 '10 at 20:39
7

In BASIC, the line numbers indicated sequence.

Also, many older editors weren't for files, but simply lines ("line editors", e.g. ed, the standard editor). By numbering them this way, you knew which line you were working on.

Justin R.
  • 23,435
  • 23
  • 108
  • 157
7

A simple google reveals what wikipedia has to say about it:

Line numbers were a required element of syntax in some older programming languages such as GW-BASIC.[2] The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which specific lines in the source code could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and commands to be executed immediately when entered by the user (which do not have line numbers).

6

Back in the day all languages had sequence numbers, everything was on punched cards. There was one line per card. Decks of cards made up your program.

When you dropped the cards, you'd put them in a card sorter that used those sequence numbers.

And of course, they were referenced by control flow constructs.

  • 1
    You should probably provide a link to say what punched cards are, because based on most of the other answers, most people here are unfamiliar with them. – Gabe Mar 12 '10 at 21:37
4

On the C64, there wasn't even a real editor (built-in at least). To edit a part of the program, you'd do something like LIST 100-200, and then you'd only be able to edit those lines that were currently displayed on the screen (no scrolling upwards!)

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • Do you have any reference for this 'editor'? – Giovanni Funchal Mar 12 '10 at 20:11
  • 1
    Yes - the editor was basically the screen memory, quite common on 8bit machines back then. – Martin Beckett Mar 12 '10 at 20:14
  • 3
    Well, it was part of the interpreter that started as soon as the C64 was turned on. It was part of the unmodifiable ROM (and it left you only with about 40k of the 64k total RAM, because it had to be copied into RAM first). It was used as a basic shell (to load programs etc), too. Maybe you want to try a C64 emulator (http://en.wikipedia.org/wiki/List_of_computer_system_emulators#Commodore_64) - it's fun :-) – Chris Lercher Mar 12 '10 at 20:19
3

They were labels for statements, so that you could GOTO the line number. The number of the statements did not necessarily have to match the physical line numbers in the file.

Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
  • In fact it was common and pretty much necessary to leave gaps of 5-10 to allow for additions to be made without having to renumber the whole program. – Buggabill Mar 12 '10 at 20:07
2

The line numbers were used in control flow. There were no named subroutines. You had to use GOSUB 60, for instance, to call the subroutine starting at line 60.


On your update, not all languages had labels, but all languages had line numbers at one time. At one time, everything was punch cards. BASIC was one of the very first interactive languages, where you could actually type something and have a response immediately. Line numbers were still the current technology.

Labels are an extra expense. You have to keep track of the correlation between the symbolic label and the code or data to which it refers. But if every line has a line number (and if all transfer of control flow statements always refer to the beginning of a line), then you don't need a separate symbol table.

Also keep in mind that original BASIC interpreters didn't need a symbol table for variables: There were 26 variables named A-Z. Some were sophisticated and had An-Zn. Some got very fancy and added a distinction between string, integer and floating point by adding "$" or "%" after the variable. But no symbol table was required.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • The issue wasn't "radical" -- assemblers had labels long before Algol-60 used them. It's the "multiple-pass" resolution of labels. Early assemblers (and compilers) had to read the entire program source several times to resolve label names. It was just slow. – S.Lott Mar 12 '10 at 20:28
  • @S.Lott: I seem to have lost my "ALGOL-60" comment. – John Saunders Mar 12 '10 at 20:34
1

IIRC, line numbers were mostly used as labels for GOTO and GOSUB statements, since in some (most?) flavors of BASIC there was no way to label a section of code.

Frank Schmitt
  • 25,648
  • 10
  • 58
  • 70
1

They were also used by the editor - ie you said:

edit 100

to edit line 100.

  • 5
    @Neil: You had an _editor_? We didn't need any _editor_! We just typed the line over again. – John Saunders Mar 12 '10 at 20:11
  • @John All versions of BASIC that I ever used, from the original Dartmouth BASIC, had a built in line editor. Certainly the ones from Microsoft (many versions) and the one on the DEC-System 10 (which is about as retro as you can get) did. –  Mar 12 '10 at 20:13
  • @Neil: was that a "line editor", or was it the interpreter? Was it a separate program? How did you start it? – John Saunders Mar 12 '10 at 20:14
  • @John You typed "BASIC" and then you were in the BASIC environment, which had a line editor. Or on microprocessor systems like (say) the Tandy CoCo or the BBC Nicro you booted immediately into the BASIC environment, also with a line editor built-in. Which version of BASIC were you using that didn't have one? –  Mar 12 '10 at 20:18
  • @John I think we may be quibling about the meaning of "line editor" - I agree that some very basic BASICs had no way of recalling the line you were trying to edit. –  Mar 12 '10 at 20:22
  • @Neil: I suppose it might have been called the editor, but it wasn't a separate program. – John Saunders Mar 12 '10 at 20:24
  • @John I didn't say it was. The editor in a modern IDE is not a separate program, but it is still called an editor. –  Mar 12 '10 at 20:26
  • 1
    @Neil: it's an issue of timeframe. Remember what an "edit report" was? At one time, maybe "editor" meant "those commands you use to edit". Today, the word is more likely meant to refer to a separate program. – John Saunders Mar 12 '10 at 20:37
1

As others have pointed out, these line numbers were used as part of subroutines.

Of course, there's a reason that this isn't done anymore. Imagine if you say GOTO 20 on line 10, and then later realize you need to write 10 more lines of code after line 10. All of a sudden, you're smashing up against 20 so you either need to shift your subroutine farther away (higher numbers) and change your GOTO value, or you need to write another subroutine that jumps farther in the code.

In other words, it became a nightmare of true spaghetti code and is not fun to maintain.

JasCav
  • 34,458
  • 20
  • 113
  • 170
1

Some editors only had an "overwrite" mode and no "insert" mode. This made editing of existing code extremely painful. By adding that line-number feature, you could however patch existing code from anywhere within the file:

100 PRINT "Broken Code"
200 PRINT "Foobar"
...
101 patch the broken code
102 patch more broken code

Because line numbers didn't have to be ordered within the file.

zneak
  • 134,922
  • 42
  • 253
  • 328
1

It was entered in on the command-line in many instances (or was, on my old Commodore 64) so there might not always have been a text editor, or if there was, it was quite basic.

In addition, you would need to do GOTOs and the like, as well as inserting lines in between others.

ie:

10 PRINT "HELLO"
20 GOTO 10
15 PRINT " WORLD"

where it would go in the logical 10 15 20

Tarka
  • 4,041
  • 2
  • 22
  • 33
1

Line numbers were a PART of the language, in some VERY early ones, even the OS was just these simple lines. ALL you had was one line at a time to manipulate. Try writing an accounting system using 1-4k program files and segmenting it by size to get stuff done. To edit you used the line numbers to tell what you were editing. So, if you entered like:

10 PRINT "howdy"
20 GOTO 10
10 PRINT "WOOPS"
15 PRINT "MORE WOOPS"
20
RUN

YOU WOULD GET:

WOOPS
MORE WHOOPS

The blank 20 would effectivly delete that line.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100