-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

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.

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.