-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?

2

2 Answers 2

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.

8
  • 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 7, 2018 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. Nov 7, 2018 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. Nov 7, 2018 at 18:04
  • 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. Nov 8, 2018 at 1:08
  • 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 9, 2018 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.