Files
akbasic/tests/language/functions/recursion.bas
Logikoma 221ed7219d Keep BASIC fixtures within the input line limit
Reflow the approved reference cases, shorten local fixture comments, and split the executable docs line so the 80-byte input buffer can read every source line.

Co-authored-by: andrew <andrew@aklabs.net>
2026-08-04 18:50:44 -04:00

24 lines
1.0 KiB
QBasic

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