Naive?
That's one of the best things *correctly* handled by the stack: a pending FOR loop in a subroutine is discarded upon return from the subroutine!
What happens here? You enter the sub, open the FOR loop, return from the sub - purging the FOR loop! - and then *errorneously* re-enter the sub routine without GOSUB. No wonders you get a ?RETURN WITHOUT GOSUB error.
Actually quite sensible behaviour. When within a subroutine, you don't need to terminate all pending FOR loops to be "allowed" a RETURN. Comes handy for early exits from search processes.
You find a particularly nice example in my TRON game, where a loop in line 41 searches for an escape path for the enemy light cycle. If it succeeds, it does an early exit - otherwise it returns with the current direction unaltered, letting the enemy crash:
Code: Select all
17 IFPEEK(G+D(H))<>219ORRND(1)<.1THENI=C:GOSUB41
[...]
41 FORT=1TO4:I=(I+1)AND3:IFPEEK(G+D(I))=219THENH=I:RETURN
42 NEXT:RETURN
Here's the game. I put it into a collection of 21 games for the unexpanded VIC-20.