Multiple RETURNS within a single subroutine
I am not sure if this is a programming style question a potential stack overflow issue.
Is it OK to have multiple RETURN statements within a single subroutine? Is there a depth limit? I have tested the multiple return subroutine up to 10 deep with no apparent problems.
/code
SubCheck: 'this is the dummy subroutine
if a = 0 then
b = 1000
return
if a = 1 then
c = 12*a
return
if j = 6 then
d = 255
return
return
'This is the calling code
code, code, code
gosub subcheck
morecode, morecode
/endcode
Cheers
Brian
It is a question of style
Having multiple return statements like that has no effect on the stack, the return statement only comes into play when it is executed.
However it's a programming style question that comes up now and again and one I contemplate again and again.
When I see horrible code like this:
Code:
IF A THEN
IF B THEN
IF C THEN
IF D THEN
do something
END IF
END IF
END IF
END IF
I want to change it to
Code:
IF NOT A THEN
RETURN
IF NOT B THEN
RETURN
...
etc
because from a debugging and comprehension standpoint I find it easier to be able to get out of the processing chain as soon as possible. I don't want to have to work through all those nested IFs wondering what's going to happen next.
It's easier for me to read, write and test.