View Full Version : Using the Return command

Jim C.
05-17-2005, 03:18 AM
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

и 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 ?

Jon Williams
05-17-2005, 03:22 AM
Return only works after GOSUB.и Using 2.5 syntax, you can do this:

и 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

05-17-2005, 03:32 AM
No, the program flow will not be correct.

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.