An interpreted language is executed one statement at a time, as they are encountered, similar to sequential operations on a calculator.
This is as opposed to a compiled language, in which the entire program is read, digested, and then executed as a new process.
Consider typing the following into a terminal, line by line:
10 GOTO 9999
20 stmt
30 stmt
40 stmt
Statement 10 is executed, but the next statement hasn't been entered yet.
Does the system just sit there swallowing everything you type until you either enter statement 9999 or an EOF sentinel?
Does execution halt when there's no target address for the branch?
Or what?
2 Answers 2
If this is an early 1980's BASIC then there is a difference between typing in the source code with line numbers, and running the said source code. Specifically, take something like Commodore 64 or ZX Spectrum BASIC: you would enter those lines and they would be stored in memory, without them being executed. Then you would separately execute a RUN command that would actually execute the source code.
Generally, languages are tokenized and parsed before being executed. They might be tokenized and parsed lazily depending on the interpreter. In that case, once execution got to the goto, then the execution would have to wait until the label 9999 had been tokenized and parsed such that execution could continue at label 9999.
Often however, even interpreted languages are fully parsed before any execution begins. So whatever was at label 9999 would have already been turned into an easier to execute form by the time the GOTO got executed.
In python if you call a function that is defined after the execution where it's called then it will throw an error:
test()
def test():
print("Hello World")
results in:
Traceback (most recent call last):
File "test.py", line 1, in <module>
test()
^^^^
NameError: name 'test' is not defined
However, you're free to define functions that call references that have yet to be defined so long as they are defined before they're called.
def test2():
test()
def test():
print("Hello World")
test2()
prints out Hello World just fine even though the definition of test2 is created using a reference to test that has yet to be defined.