PDA

View Full Version : Programming style - GOTO or GOSUB ?



AndrewC
- 6th December 2010, 14:49
I'm looking for GO GO Guru :)

In pbp, what are the tradeoffs between using GOTO or GOSUB to branch to different areas of code ? Does one use more memory than the other ? Is there a limit to how many GOSUBs that can be nested ?

I have some distant memory that you should only use GOTO within a specific (sub)routine and it is better to use GOSUB rather than GOTO to jump to / use a specific code segment. Obviously as well GOSUB will return you to where you started whereas with GOTO you can jump A>B>C>A.

Also, I often use a WHILE - WEND loop for 3 button menu control, is it better to use GOTO or GOSUB here:

WHILE Switch_1 = 1
IF Switch_2 = 0 then GOSUB Label X
IF Switch_3 = 0 then GOSUB Label Y
WEND


Thanks, Andrew

mackrackit
- 7th December 2010, 17:00
I am no guru, but here is how I see it.

GOSUB -- There is a limit to how many GOSUBs that can be nested for different core devices because of stack capabilities. The manual talks about the limits. Other than nesting you can have as many as you want. Because info is written to the stack so the code knows where to RETURN to a certain number of extra cycles will be used. How many I am not sure.

GOTO -- Does not use the stack so the above "problems" are not a consideration. Some say a program with many GOTOs is some type of pasta programing.

WHILE/WEND -- Is basically a GOTO after the WEND. So a GOTO or GOSUB is OK. As long as the stack/nesting problem with GOSUBs is considered. No nest, no problem.

So basically, IMHO, it comes down to program style. Many use to say the code should "flow" with out the need for GOTOs. I find myself coding in blocks more and more and GOSUBing to where I want. Maybe this is from learning more about other languages that are Object Oriented? My "MAINLOOP" will often be a bunch of GOSUBs.

AndrewC
- 7th December 2010, 21:29
Thanks for the answer. I used to use GOSUBs for pretty much everything but now I keep it for oft visited chunks and use GOTOs to navigate a more linear path. I think I tripped myself up once by going down to many .SUBs and lost the way home :(

Charles Linquis
- 8th December 2010, 06:08
18Fs using PBP have a 27 level stack. That is a lot of GOSUBs.

Whatever you do - keep track of your GOSUBs. Don't ever GOSUB to a routine and then use
GOTO to get "back".

Every time you GOSUB, you put an address on the stack. Every time you RETURN, you pop that address and resume execution there. If you push addresses there without pop-ing them off, you will eventually get a stack overflow which causes a processor reset.

Ioannis
- 8th December 2010, 10:16
If you have functions or routines used often in your program, then gosub is a must.

It helps keep the program structured and by carefully placing the subroutine in a complete page of pogram memory, the overall program can be reduced in size very much.

It would be nice to see local variables sometime in the next updates of PBP,but even without this, GOSUBs are my preference.

Other languages like C "push" the programmer to use functions or subroutines.

Ioannis

tenaja
- 8th December 2010, 16:31
I'm looking for GO GO Guru :)

In pbp, what are the tradeoffs between using GOTO or GOSUB to branch to different areas of code ? Does one use more memory than the other ? Is there a limit to how many GOSUBs that can be nested ?

I have some distant memory that you should only use GOTO within a specific (sub)routine and it is better to use GOSUB rather than GOTO to jump to / use a specific code segment. Obviously as well GOSUB will return you to where you started whereas with GOTO you can jump A>B>C>A.

Also, I often use a WHILE - WEND loop for 3 button menu control, is it better to use GOTO or GOSUB here:

WHILE Switch_1 = 1
IF Switch_2 = 0 then GOSUB Label X
IF Switch_3 = 0 then GOSUB Label Y
WEND


Thanks, Andrew

Using Gosub or Goto is NOT a style question. It is a FLOW question.

You use Gosub when you want to Return as soon as the routine is done.

Use Goto to depart your routine, "never" to return. (Although you can start it over later.)

AndrewC
- 8th December 2010, 18:22
So should there be any difference in the amount of memory used when coding ? Empirically they seem to use the same amount of compiled memory.

mackrackit
- 8th December 2010, 19:08
GOSUB will use just a little more space.

Example...
I am working a piece of code with two sub routines that when finished will be part of the main code, a flow through thing. I have them as GOSUBs now for testing, easily comment out a GOSUB to test one or the other.

As the code stands currently with the GOSUBs and RETURNs in place 3528 bytes are used.
When I remove the GOSUBs and RETURNs 3516 bytes are used.

The extra space is small compared to having a "function" written over and over.
The way to have the smallest code is this respect is to have one loop, but if you have an ADC that needs read more than once in the main loop a GOSUB/RETURN will be much smaller than having the ADC written twice... or more.