Shop OBEX P1 Docs P2 Docs Learn Events
IF...THEN and RETURN — Parallax Forums

IF...THEN and RETURN

SteveDSteveD Posts: 64
edited 2005-12-17 01:05 in General Discussion
I understand that when a GOSUB instruction sends the program to a subroutine that contains a RETURN command the program will return to the next instruction following the initial GOSUB instruction.

My question is if a GOSUB instruction sends the program to a subroutine that contains an IF…THEN statement that sends the program to another subroutine which contains a RETURN, will the program go back to the next instruction following the IF…THEN or will it be directed to the instruction following the initial GOSUB instruction?
·
Thanks

Comments

  • BeanBean Posts: 8,129
    edited 2005-12-15 19:07
    Steve,
    When you say "send the program to another subroutine" do you mean with GOTO or with GOSUB.
    If you use GOSUB then it will return back to the IF...THEN if you use GOTO it will return back to the calling program.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Forget about the past, plan for the future, and live for today.
    ·
  • nick bernardnick bernard Posts: 329
    edited 2005-12-15 19:13
    how many nested gosubs can the sx handle?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    engineer, fireman, bowler, father, WoW addict [noparse];)[/noparse]
  • Mike CookMike Cook Posts: 829
    edited 2005-12-15 19:17
    8

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "OEM NMEA GPS Module" Now available on ebay for only $19.99, FREE shipping until 2006!

    Product web site: http://www.allsurplus.net/Axiom/
  • SteveDSteveD Posts: 64
    edited 2005-12-15 19:19
    Main:
    temp
    temp1
    temp2
    GOTO main

    TEMP:
    code
    return


    TEMP1:
    IF "statement true" THEN temp
    RETURN

    TEMP2:
    code
    RETURN

    I believe I am referring to GOSUB.
  • BeanBean Posts: 8,129
    edited 2005-12-15 19:32
    Steve,
    "THEN temp" is the same as "GOTO temp" so that will NOT return back to the line after the "IF".
    If you want it to then do this:

    IF "statement true" THEN
    temp
    ENDIF

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Forget about the past, plan for the future, and live for today.
    ·
  • SteveDSteveD Posts: 64
    edited 2005-12-16 05:29
    Thank you Bean,
    I do not want it to return back to the line following the IF.
    So when the IF instruction sends to temp where will it go after it comes to the RETURN that is in the temp subroutine? I was thinking it would continue on to temp2. Would the following be the correct flow of the example program?

    (GOSUB) temp
    temp (subroutine)
    code
    return
    (GOSUB) temp1
    IF…THEN (GOTO) temp
    temp (subroutine)
    code
    return
    (GOSUB) temp2
    code
    return
    goto main
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-12-16 11:15
    Steve,

    RETURN will send the program back to the line after the [noparse][[/noparse]implied] GOSUB that was last used. You can have conditionals that use GOTO in the mix.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BeanBean Posts: 8,129
    edited 2005-12-16 11:54
    Steve, I'm not sure what your getting at without a "real-world" example.

    Here is some stuff I do to save code space.

    Let's say I need a routine to turn on an LED and then delay for 10mSec.
    I also need another routine to turn off the LED and then delay for 10mSec.
    Then I also need anouther routine to just delay for 10mSec.

    Okay so here is some very space effecient code:
    LEDon SUB
    

    LEDoff SUB
    

    Delay10 SUB
    

    
    
    LEDon:
    

      HIGH LED
    

      GOTO Delay10 ' Now even though Delay10 is a SUB this will JUST jump to the label
    

    
    
    LEDoff:
    

      LOW LED
    

    
    
    Delay10:
    

      PAUSE 10
    

      RETURN
    


    Sorry about lines, I don't know how I made them appear ???
    Notice how LEDoff will just "fall-into" the Delay10 routine.

    I hope this helps explain it.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Forget about the past, plan for the future, and live for today.


    Post Edited (Bean (Hitt Consulting)) : 12/16/2005 11:59:29 AM GMT
  • SteveDSteveD Posts: 64
    edited 2005-12-17 01:00
    Thanks guys I think I got it. The last GOSUB is remembered and regardless of any IF..THEN GOTO's the program will return back to the line following the last remembered GOSUB.
    It appears that I was trying to make it more difficult than it is, but I needed positive reinforcement to be sure.
  • BeanBean Posts: 8,129
    edited 2005-12-17 01:05
    Yep, that's how it works.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Forget about the past, plan for the future, and live for today.
    ·
Sign In or Register to comment.