Programming style - GOTO or GOSUB ?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104

    Default Programming style - GOTO or GOSUB ?

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    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.
    Charles Linquist

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by AndrewC View Post
    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.)

  7. #7
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    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.

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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.
    Dave
    Always wear safety glasses while programming.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts