-1

Admittedly it's probably mostly in traditional BASICs (which require line numbers) that I've seen this. Take these examples (cribbed from Wikipedia):

10 PRINT "Hello, World!"
20 END

and

10 INPUT "What is your name: "; U$
20 PRINT "Hello "; U$
30 INPUT "How many stars do you want: "; N
40 S$ = ""
50 FOR I = 1 TO N
60 S$ = S$ + "*"
70 NEXT I
80 PRINT S$
90 INPUT "Do you want more stars? "; A$
100 IF LEN(A$) = 0 THEN GOTO 90
110 A$ = LEFT$(A$, 1)
120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
130 PRINT "Goodbye "; U$
140 END

In each of these examples, the END isn't doing anything, since the program will halt anyway as a result of reaching the end of the code. Neither is there a GOTO statement or anything referencing the line. In C and similar languages, I don't recall ever seeing return; immediately before the closing brace of a function, or exit(0); immediately before the closing brace of main.

What is the origin of this habit? Have there been some BASIC dialects that require it? Or have some been taught that it's a good practice for whatever reason?

Stewart
  • 155
  • 4
  • 7
    Perhaps a better fit on [Retrocomputing](https://retrocomputing.stackexchange.com/)? – Philip Kendall Nov 06 '18 at 22:38
  • Probably from the era of https://en.wikipedia.org/wiki/Type-in_program , where magazine readers would manually type in tens of pages of source code or interpreter byte code found on printed magazines. – rwong Nov 06 '18 at 22:45

2 Answers2

13

Because the program might contain subroutines or functions after the end statement. That is, in more complex programs, the logical end of the program may not be the end of the file:

10 PRINT "Hello, World!"
15 GOSUB 30
20 END
30 PRINT "Goodbye World!"
40 RETURN

This example is a silly use of a subroutine, but it makes more sense when the subroutine contains complex logic that is called multiple times.

In C and similar languages, I don't recall ever seeing return; immediately before the closing brace of a function, or exit(0); immediately before the closing brace of main

You haven't seen many ANSI C programs then. Omitting the return from main resulted in "undefined behavior" before C99.

Charles E. Grant
  • 16,612
  • 1
  • 46
  • 73
  • But the examples I've posted clearly don't have subroutines or functions after the end statement, hence my question. Furthermore, C programmers would have avoided the undefined behaviour by returning a value, rather than by returning nothing, surely? Indeed, on that page I see: "Reaching the } that terminates a function is equivalent to executing a return statement without an expression." – Stewart Nov 07 '18 at 13:34
  • @Stewart flavors of BASIC varied considerably back then. As MikeRobinson points out, in some interpreters the END was a required element of the syntax. Other interpreters could be used in batch mode or interactive mode, and if your program didn't include an explicit END the interpreter would sit twiddling its thumbs waiting for further input. Not a great thing in batch mode. In those interpreters that didn't require it, it was still encouraged as best practice so that you wouldn't leave it out when it was necessary. – Charles E. Grant Nov 07 '18 at 17:50
  • @Stewart 'C programmers would have avoided the undefined behavior by returning a value, ' Ah I see now. I missed the fact that you were referring literally to 'return;' not 'return i;' in general. This is just the syntax chosen by the language designer. Yes, in C, return is not required for syntactical completeness of a program. Other languages make different choices. Many flavors of Pascal require that programs be enclosed in 'program' and 'end.' keywords. Back in the day some operating system didn't make it so easy to treat the end of a file as just one more characters in the input stream. – Charles E. Grant Nov 07 '18 at 18:04
  • You mean in some old BASICs, END was the way of telling the interpreter "I've finished keying in my program, now run it", rather than being a statement executed as part of the program? This must have caused some confusion. Still, I imagine that nearly all BASIC code we would see nowadays would be from the "END is a statement that halts execution" era or a later one.... – Stewart Nov 07 '18 at 23:47
  • 'Still, I imagine that nearly all BASIC code we would see nowadays would be from the "END is a statement that halts execution" era or a later' I don't see why. Pretty much all the BASICS under active development switched to structured code sans line numbers in the middle 80s: Quick Basic, True Basic, etc. Any BASIC code you are seeing with line numbers is pretty old stuff. – Charles E. Grant Nov 08 '18 at 00:58
  • 1
    "You mean in some old BASICs, END was the way of telling the interpreter "I've finished keying in my program, now run it" No, RUN was generally the command to start your program executing, but if your program didn't contain an "END" you'd be left in the interpreter, awaiting more commands like RUN, LOAD, SAVE, and LIST. This is probably what you wanted if you were working in interactive mode, but not in batch mode. – Charles E. Grant Nov 08 '18 at 01:08
  • Interesting. Clearly this is dialect-dependent, so it's puzzling that it would have caught on as a 'best practice'. People used to dialects where END simply means "halt the program" must have got caught out when they came across one in which it means "exit back to the OS". On that basis, I would have thought people would have been told to avoid using it (except for possibly adding it in once you've otherwise finished writing the program, which would make it pretty useless if you want it anywhere but at the end of the code). – Stewart Nov 09 '18 at 22:29
  • 1
    "Any BASIC code you are seeing with line numbers is pretty old stuff." Yes, such code is still around, e.g. on the aforementioned Wikipedia page, Computer Stupidities, and various retrocomputing resources. There are probably many retrocomputing enthusiasts who even still _write_ such code. – Stewart Nov 09 '18 at 22:33
5

Furthermore, in some BASIC interpreters that I have used ... ("HP2000 Access BASIC," anyone? Bah ... these kids today ...) ... the END statement was required at the end of the program, and nowhere else.

LAST STATEMENT NOT "END" IN LINE 7530 ... ahh yes, I remember it well.

Mike Robinson
  • 1,765
  • 4
  • 10