Files
akbasic/tests/language/functions/recursion.bas
Logikoma eb93bb7da0
All checks were successful
akbasic CI Build / cmake_build (push) Successful in 3m29s
akbasic CI Build / sanitizers (push) Successful in 4m42s
akbasic CI Build / coverage (push) Successful in 3m56s
akbasic CI Build / akgl_build (push) Successful in 8m34s
akbasic CI Build / mutation_test (push) Successful in 18m50s
Fix 80-column fixtures and tutorial expectations
2026-08-04 16:36:27 -04:00

24 lines
1.1 KiB
QBasic

10 REM A DEF call takes one environment from the pool, 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: 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, so runaway recursion reports
190 REM "Environment pool exhausted" rather than hanging. It is not exercised
200 REM here because the failed statement still prints junk afterwards;
210 REM this is a separate defect in TODO.md, and one this golden file
220 REM would pin if it went in. tests/user_functions.c asserts
230 REM the message instead.