In other languages you can't call into the middle of a function. There is
one entry point, possibly multiple exit points. There's no chance
unbalanced CALL and RETURN. once you return you're jump to the stacked
address and you're no longer in the subroutine.

In basic subroutines, because every line is a potential entry point, you
can possibly have multiple entry points into the same subroutine! Or in
this case that follows, a single entry point but in one case I enter the
sub at line 1000 with a gosub and in the other with sequential "fall
through" execution.

100 gosub 1000

1000 ?"hello"
1010 return

That will give you the same error you saw, Peter, because it unbalanced the
return stack on the second entry to line 1000.

How can I avoid it?

Add

110 goto 100

It will run forever.

So to avoid the fault, always enter your subroutines through a single entry
point, and always using a gosub.

Also be careful to avoid GOSUBing the routine from within the routine,
that's recursion. IIRC I don't think the BASIC stack is very deep, so
although recursion is a legitimate technique I don't think it's wise in
BASIC. I could be wrong.

Either way that's another story.

-- John.

On Mon, Dec 10, 2018, 12:13 AM Peter Vollan <[email protected] wrote:

> That is just a more verbose way of saying what I already said.
> On Sun, 9 Dec 2018 at 19:04, John R. Hogerhuis <[email protected]> wrote:
> >
> > "  It seems that a limitation of the model
> > 100 is that if you gosub to a subroutine, and then gosub again, it
> > will forget that it is it in a subroutine, and not understand the
> > retrurn statement."
> >
> > That's not how it works. GOSUB stacks addresses. It means your gosubs
> and returns are not balanced, which is a bug in your code.
> >
> > Think of a GOSUB as pushing the location of the next BASIC command onto
> a stack.
> >
> > At some point, you RETURN and it pops that location from the stack and
> jumps there.
> >
> > If you get a "RETURN without GOSUB" it means you tried to RETURN when
> the stack was empty!
> >
> > -- John.
>

Reply via email to