Why BASIC had numbered lines? [duplicate]
Asked Answered
S

15

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?

Soccer answered 12/3, 2010 at 20:2 Comment(8)
When I loaded the page, there were no answers. When I finished typing mine up, there were six, including mine. Answer overflow.Clough
I think you may have hit the nostalgia button here... Lots of fond memories from days gone by...Vanadinite
Duplicate: #541921Odessaodetta
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).Rajiv
@NealB: Oh yeah. Almost felt goosebumps when typing my answer :)Libelous
@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.Teniacide
Why ask us? Ask Dr. Kurtz -- one of the inventors of BASIC. en.wikipedia.org/wiki/Thomas_Eugene_KurtzNoh
@John I agree, but they provide a point of reference in much the same way that line numbers did back in the day.Rajiv
S
30

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!

Synesthesia answered 12/3, 2010 at 20:39 Comment(3)
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.Twelvemo
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.Endpaper
@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.Synesthesia
D
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).

Dochandorrach answered 12/3, 2010 at 20:3 Comment(8)
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.Folksy
@roygbiv: that was indeed the most important reason for the existence of line numbers in BASICLibelous
@Philippe: "an important reason", not the most important. Programs were not interesting without GOTO or GOSUB.Teniacide
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.Rajiv
There were many reasons for line numbering, mainly historic. The interactive editing was a useful reason, but not the only one....Dochandorrach
@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.Dochandorrach
So far in this thread there are two reasons: GOTO/GOSUB and editing... what are some other reasons?Folksy
@John: you're right. Wrong wording. There were 2 main reasons: line ordering and labeling for GOTO and GOSUBLibelous
L
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)

Libelous answered 12/3, 2010 at 20:3 Comment(12)
("So if you typed") Can you name the text editor you are referring to please?Soccer
There was no text editor. Just a command line where you would type your codeLibelous
Yes but is there any name for this command line? I can't find any reference of it on the web, no screenshot, nothing.Soccer
@Helltone: BASIC was an interpreted language. You typed into the interpreter.Teniacide
@JohnFx: What "edlin" are you talking about? What platform was that?Teniacide
@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: codeazur.com.br/stuff/fc64_finalLibelous
@John edlin was a line editor that came with the first version of MS-DOS - it had nothing to do with BASIC.Ostracism
@Neil: I'm so confused. So why are we talking about edlin in conjunction with BASIC, which predates MS-DOS by over a decade?Teniacide
@John "edlin" was a specific (microsoft) product - a "line editor" is a generic idea for how to edit text.Ostracism
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...Libelous
@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.Ostracism
@Neil: I've used edlin to write C programs for the IBM PC XT.Teniacide
D
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.

Damages answered 12/3, 2010 at 20:4 Comment(0)
F
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).

Folksy answered 12/3, 2010 at 20:7 Comment(0)
B
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.

Betts answered 12/3, 2010 at 20:6 Comment(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.Sidman
W
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!)

Whyalla answered 12/3, 2010 at 20:6 Comment(3)
Do you have any reference for this 'editor'?Soccer
Yes - the editor was basically the screen memory, quite common on 8bit machines back then.Convector
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 (en.wikipedia.org/wiki/…) - it's fun :-)Whyalla
T
3

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.

Teniacide answered 12/3, 2010 at 20:3 Comment(2)
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.Noh
@S.Lott: I seem to have lost my "ALGOL-60" comment.Teniacide
C
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.

Clough answered 12/3, 2010 at 20:4 Comment(1)
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.Smearcase
L
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.

Leflore answered 12/3, 2010 at 20:3 Comment(0)
O
1

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

edit 100

to edit line 100.

Ostracism answered 12/3, 2010 at 20:6 Comment(8)
@Neil: You had an editor? We didn't need any editor! We just typed the line over again.Teniacide
@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.Ostracism
@Neil: was that a "line editor", or was it the interpreter? Was it a separate program? How did you start it?Teniacide
@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?Ostracism
@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.Ostracism
@Neil: I suppose it might have been called the editor, but it wasn't a separate program.Teniacide
@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.Ostracism
@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.Teniacide
S
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.

Semiannual answered 12/3, 2010 at 20:6 Comment(0)
K
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.

Kestrel answered 12/3, 2010 at 20:6 Comment(0)
I
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

Interbreed answered 12/3, 2010 at 20:7 Comment(0)
A
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.

Allocation answered 12/3, 2010 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.