can i jump into a gosub, then out early, using labels?

Started by stevmjon, October 17, 2009, 06:10:55 PM

Previous topic - Next topic

stevmjon

hi

just wondering about possible stack overflow. i jumped into a part of a gosub using 'goto label:' , read a small portion of it, then used 'goto label:' to jump back out to where i started from.

will this cause the same problem as not using return in a gosub?

i figured if i didn't use 'gosub label' it would be o.k. but i want to make sure.

thanks stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


Without seeing the code,  all i can say is that this is potentially problematic design.


kevin

 Examples...


PlayBASIC Code: [Select]
  // Single Call point, Multiple Exit points..

Do
cls 0

for lp=0 to 10000
inc x
Gosub SubRoutine
next

print X

Sync
loop


SubRoutine:

if X>1 then return
print "X not greater than 1"

return







  Using a goto inside the subroutine to jump to the exit point..  This is legal, but ugly as hell..

PlayBASIC Code: [Select]
  // Single Call point, Single Exit..  

Do
cls 0

for lp=0 to 10000
inc x
Gosub SubRoutine
next

print X

Sync
loop


SubRoutine:

if X>1 then goto SubRoutineEnd
print "X not greater than 1"

SubRoutineEnd:
return





PlayBASIC Code: [Select]
  // Single Call point, Single Exit..  

Do
cls 0

for lp=0 to 10000
inc x
Gosub SubRoutine
next

print X

Sync
loop



// one entry point
SubRoutine:

if X<=1
print "X not greater than 1"
endif

// one exit point
return






 
  This one has two entry points, and one exit point.  Regardless of entry point the code will flow through to the single exit point.  So it's legal.. ugly, but legal...

PlayBASIC Code: [Select]
   Do
cls 0

for lp=0 to 10000
inc x
Gosub SubRoutine_EntryPoint1
next

for lp=0 to 10000
Gosub SubRoutine_EntryPoint2
next

print X

Sync
loop



// first entry point
SubRoutine_EntryPOint1:

if X<=1
print "X not greater than 1"
endif

// second entry point
SubRoutine_EntryPOint2:


// one exit point...
return





   However if you goto into a subroutine, and expect to exit via Return, then this won't work.

PlayBASIC Code: [Select]
   Do
cls 0

for lp=0 to 10000
inc x
Gosub SubRoutine_EntryPoint1
next

for lp=0 to 10000
Goto SubRoutine_EntryPoint2
next

print X

Sync
loop



// first entry point
SubRoutine_EntryPOint1:

if X<=1
print "X not greater than 1"
endif

// second entry point
SubRoutine_EntryPOint2:


// one exit point...
return




stevmjon

thanks kev

the way i was thinking was slightly different to your examples. to enter via label, then exit via label, upon condition only.


do

   gosub subroutine_normal

   if condition=1 then goto enter_part_gosub
   exit_part_gosub:

   sync

loop


subroutine_normal:

   do stuff 1

enter_part_gosub:

   do stuff 2

   if condition=1 then goto exit_part_gosub

return
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


   I had a feeling that's what you we're asking.  To be blunt, that's a pretty awful  programming practice as your example is emulating a Gosub for no worthwhile  reason.

stevmjon

i figured as much, that's why i asked.  i was merely experimenting.

i just wanted to occasionly jump into part of a gosub, read one small portion, then jump out.

as you say, emulating a gosub is not good practice, so i won't be lazy, i will stick to normal practice.

thanks for your advice,  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.