Subroutines running, repeating, without being called
anticipate
Posts: 10
Unfortunately, I didn't include in title that this is a question about PBASIC for BOE Bot.
I expect this program to have NO output, because the subroutine is never being called. And I don't understand why it would loop.
Instead, this code enters the subroutine and then repeats the subroutine indefinitely. What is the proper way to use a subroutine?
Output:
I'm a naughty subroutine
I'm a naughty subroutine
. . . ad infinitum
I expect this program to have NO output, because the subroutine is never being called. And I don't understand why it would loop.
Instead, this code enters the subroutine and then repeats the subroutine indefinitely. What is the proper way to use a subroutine?
Sample code: ' {$STAMP BS2} ' {$PBASIC 2.5} naughtySubroutine: DEBUG "I'm a naughty subroutine", CR RETURN
Output:
I'm a naughty subroutine
I'm a naughty subroutine
. . . ad infinitum
Comments
Welcome to the Parallax forum! You will need to post your entire program in order for us to help you.
'Just a guess, though: you probably don't have an END statement between your main program and the subroutine.
Thanks,
-Phil
Put your main code above your subroutines, use and END statement after the main code to ensure the subroutine can't run without being called by a GOSUB.
The code I posted was complete in that it was sufficient to answer my question, and yes Phil, you were right about the answer.
Sapphire, thank you very much for the succinct and complete answer to my question. It makes absolutely no sense to me that the program would enter a subroutine without it being called, so I had made the assumption that it would not. Is there a practical reason for that behavior, or an application?
For future readers, the proper way to use a subroutine (please correct me if I am wrong):
Code is code, and it all executes in a linear fashion (top to bottom) unless the programmer directs otherwise.
"Is there a practical reason for that behavior..."
In your first example, the "subroutine" was just linear code because it hadn't been called...and so entered the observed infinite loop. In the corrected version, the code became a subroutine because it was related to the naughtySubroutine lable and called by the GOSUB command.
GOTO
GOSUB
RETURN
I found that reference by pressing F1 in the BASIC Stamp editor and clicking on PBASIC Language Reference in the left pane.
Apologies - in regards to PBasic, code is code, linear, top to bottom. The Stamp never saw a subroutine because there was no "GOSUB-to-a-label". So the Stamp executed as it normally does, top-to-bottom, and did what was told to do; in this case "RETURN-to-the-top" and start over.
Yup - you got it. :thumb: Both GOSUB and GOTO commands target labels.
No prob! :cool:
I'm trying to get a reasonable grasp on this language because I'm volunteering to teach a bunch of Jr High / HS kids that really aren't that interested, and if they chew me up, I don't want it to be because I have no idea what I'm talking about.
It may not be the prettiest or the most powerful of Basic languages or the most politically correct, but it has served its purpose. There are competitive BasicStamp like products out in the marketplace that offer much more elaborate Basic languages that are finely crafted, but it begins to be difficult to get any work done through the elaborate language specification when you just want to build a little project.
That makes a lot of sense! Thanks for the perspective.