24 lines
1.1 KiB
QBasic
24 lines
1.1 KiB
QBasic
|
|
10 REM A DEF call takes its environment from the pool, one per call, exactly as
|
||
|
|
20 REM GOSUB does. It used to be owned by the funcdef and reset on every call,
|
||
|
|
30 REM which cost two silent defects: two calls in one expression shared a slot,
|
||
|
|
40 REM and recursion never came back at all.
|
||
|
|
50 DEF FACT(N#)
|
||
|
|
60 IF N# <= 1 THEN RETURN 1
|
||
|
|
70 RETURN N# * FACT(N# - 1)
|
||
|
|
80 PRINT FACT(5)
|
||
|
|
90 REM Each frame keeps its own argument, which is what makes the unwind work.
|
||
|
|
100 DEF SUMTO(N#)
|
||
|
|
110 IF N# <= 0 THEN RETURN 0
|
||
|
|
120 RETURN N# + SUMTO(N# - 1)
|
||
|
|
130 PRINT SUMTO(4)
|
||
|
|
140 REM And two calls to one function in one expression no longer collide.
|
||
|
|
150 DEF DBL(N#) = N# * 2
|
||
|
|
160 PRINT DBL(10) + DBL(1)
|
||
|
|
170 PRINT DBL(1) + DBL(10) + DBL(100)
|
||
|
|
180 REM Depth answers to the environment pool now, so runaway recursion reports
|
||
|
|
190 REM "Environment pool exhausted" rather than hanging. It is not exercised here
|
||
|
|
200 REM because the statement containing a failed call still prints a junk value
|
||
|
|
210 REM afterwards -- a separate, pre-existing defect recorded in TODO.md, and one
|
||
|
|
220 REM this golden file would pin if it went in. tests/user_functions.c asserts
|
||
|
|
230 REM the message instead.
|