Shop OBEX P1 Docs P2 Docs Learn Events
Newcomer can't understand why subroutines don't work with RETURN — Parallax Forums

Newcomer can't understand why subroutines don't work with RETURN

glennwglennw Posts: 8
edited 2007-01-11 23:04 in BASIC Stamp
I have a 'basic' question about the behavior of my subroutines. I am writing a program that interfaces to an A/D converter and LCD display to measure analog voltages. My main program loop looks like this:
'''''''''''''''''''''''''''
'define and initialize variables and constants
'dislay welcome screen
'set page
'''''''''''''''''''''''''''
Main:
PAUSE 100
BUTTON Btn1, 0, 255, 250, btnWrk, 1, Backlight
BUTTON Btn2, 0, 255, 250, btnWrk, 1, DisplayMode
loopcount = loopcount + 1
mux_ch = ch0
GOSUB Read_0834
volts = param
mux_ch = ch1
GOSUB Read_0834
chrg_amps = param
mux_ch = ch2
GOSUB Read_0834
load_amps = param
IF loopcount = 10 THEN
loopcount = 0
GOTO Update_display
ENDIF
GOTO Main

Every 10 main program loops I go and update my display with the latest A/D values. My Read_0834 subroutine works as expected and uses a RETURN statement. My Update_display subroutine looks like this:

Update_display:
''''''''''''''''''''''''Set & scale Volts, Amps'''''''''''''''''''''''''''''''''
'set and scale volts, chrg_amps, load_amps
'''''''''''''''''''''''Display results'''''''''''''''''''''''''''''''''''''
IF page = 2 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Chrg amps position
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps1, DEC chrg_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps3]
ELSEIF page = 3 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Drain amps position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ELSEIF page = 1 THEN
SEROUT 15, 32, [noparse][[/noparse]139] 'move cursor to charge A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write load_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
SEROUT 15, 32, [noparse][[/noparse]159] 'move cursor to drain A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ENDIF
RETURN

When the RETURN statement is used, the subroutine exits to the beginning of the program (and displays my welcome screen). If I use a GOTO main statement at the end of the subroutine, then it functions as I expect.

My Backlight and DisplayMode subroutines from the BUTTON command have a similar problem with RETURN.

Any help would be appreciated.

Glenn

Comments

  • CJCJ Posts: 470
    edited 2007-01-11 18:50
    instead of goto, use gosub to call update_display, you can't return from a goto

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-01-11 20:05
    Good catch. If you RETURN from a routine you reached with a "GOTO", you 'underflow' the return stack and restart your program.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-01-11 22:04
    Allan -

    That can and may happen as it probably does in this case. That isn't true in the general case however. It really returns to the next return location in the return stack. In this case it happens to be the beginning of the program.

    I just disn't want folks to think this was a tricky way to return to the beginning of the program.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • PARPAR Posts: 285
    edited 2007-01-11 22:39
    glennw said...
    I have a 'basic' question about the behavior of my subroutines. I am writing a program that interfaces to an A/D converter and LCD display to measure analog voltages. My main program loop looks like this:

    ...
    Main:
    PAUSE 100
    BUTTON Btn1, 0, 255, 250, btnWrk, 1, Backlight
    BUTTON Btn2, 0, 255, 250, btnWrk, 1, DisplayMode

    ...
    My Backlight and DisplayMode subroutines from the BUTTON command have a similar problem with RETURN.

    Any help would be appreciated.

    Glenn
    Others commented on the Subroutine call.

    Regarding the Button command use, "Backlight" and "DisplayMode" are statement labels (addresses)·to which your program will branch if the condition (state) of the Btn is met.

    Your program will then continue executing at that label, and where it goes next (sequentially to the next statement, or branching elsewhere) depends on how you structure that portion of your program.· The place to which your Button command branches is not a subroutine, and will not have a Return. So, for example, replace those Return statements with some appropriate GoTo (or other flow control)·statements.

    PAR
    ·
  • glennwglennw Posts: 8
    edited 2007-01-11 23:04
    Thanks all for the input. I quickly realized my error re: goto vs gosub, but then had to read the BUTTON command more closely to see the reference to 'branch to label.' I think I understand all my current problems now.

    I appreciate all the responses. This is a great forum!
Sign In or Register to comment.