PDA

View Full Version : Multiple RETURNS within a single subroutine



BrianT
- 20th June 2009, 00:00
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

Jerson
- 20th June 2009, 04:42
Perfectly fine.

sougata
- 20th June 2009, 14:12
Hi,

Since any logic validation executes the return it is as good as only one return.

ukemigrant
- 21st June 2009, 16:43
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:


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


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.