Using the Return command
Jim C.
Posts: 8
Has anyone ever used the return command after the IF THEN statement ? What I mean is this. I want to test something so I do... IF variable_1 > variable_2 THEN do this. At the end of the, then do this, routine I want the program to go back to where it was so I want to use the RETURN command. Usually I would use the RETURN command at the end of a routine that was initiated by a GOSUB not a IF THEN statement.
Here is actual code.
INPUT 5····································'check for brake input
· IF· PIN5 = 0 THEN Brake··········· 'disable system if brake is on
· IF MPH < 35 THEN Brake··········· 'disable system if speed drops below this preset limit
·
Brake:
· Enabled=0························ 'Clear the system enabled bit
· LOW 4···························· 'Turn off Torque converter solenoid
· LOW 6···························· 'Turn off system enabled indicator
· Return
The editor doesn't give me an error so it is happy. Will the program flow be correct ?
Here is actual code.
INPUT 5····································'check for brake input
· IF· PIN5 = 0 THEN Brake··········· 'disable system if brake is on
· IF MPH < 35 THEN Brake··········· 'disable system if speed drops below this preset limit
·
Brake:
· Enabled=0························ 'Clear the system enabled bit
· LOW 4···························· 'Turn off Torque converter solenoid
· LOW 6···························· 'Turn off system enabled indicator
· Return
The editor doesn't give me an error so it is happy. Will the program flow be correct ?
Comments
· IF (BrakeChk = IsOn) THEN GOSUB Brake
· IF (mph < 35) THEN GOSUB Brake
Note that using embedded constants and pin names is an easy way to get into trouble as your program grows.· In my version of your code, you would need these definitions:
BrakeChk··· PIN··· 5
IsOn······· CON··· 0
IsOff······ CON··· 1
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
What will happen (eventually) is you will 'Return' when you have not done a 'gosub'. The effect of this (I believe) is a reset of the BS2. This is also called 'underflow of the Return Stack', when you pop off more return addresses (by doing RETURN commands) than you have put there (by doing GOSUB).
The reason PBasic can't detect this as a syntax error is that PBasic doesn't draw a distinction between a GOTO destination label, and a GOSUB destination label. It's up to you as a responsible programmer not to 'RETURN' from a GOTO label.