Qbasicnews.com

Full Version: gosub stack overflow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
got a different question. .ok here goes.. i know if you use gosub too much it can cause a stack overflow... but what if i use subroutines... and dont actually use gosub??? or when i use the subroutine is it the same as using gosub.. .. grrrraaaahhh!!! my head hurts
You only cause a stack overflow if you use GOSUB without using RETURN

If you use a SUB that calls itself without ending, you will get the same effect, namely STACK OVERFLOW.

examples, causes overflow:
GOSUB:
Code:
1 Do
Gosub 1
Loop
SUB/FUNCTION:
Code:
declare sub foo

foo

sub foo
foo
End sub

GOSUB puts the return adress (the current position in the program) on the stack, so you can return to where you last used GOSUB by using RETURN.

When you call a SUB/FUNCTION the same thing is done, so that when the SUB/FUNCTION is done it jumps to the instruction immediately after the sub jump.