Shop OBEX P1 Docs P2 Docs Learn Events
Subroutines running, repeating, without being called — Parallax Forums

Subroutines running, repeating, without being called

anticipateanticipate Posts: 10
edited 2013-11-18 18:39 in General Discussion
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?
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

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-11-14 18:35
    anticipate,

    Welcome to the Parallax forum! You will need to post your entire program in order for us to help you.

    attachment.php?attachmentid=78421&d=1297987572

    'Just a guess, though: you probably don't have an END statement between your main program and the subroutine.

    Thanks,
    -Phil
  • SapphireSapphire Posts: 496
    edited 2013-11-14 19:08
    Subroutines are generally placed at the end of your program, after the main code. If you have it at the top, the program "falls" into the subroutine, and executes that code. When it reaches a RETURN without a prior GOSUB, it doesn't know where to return to, and the program starts over at the top. Hence your repeating subroutine.

    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.
  • anticipateanticipate Posts: 10
    edited 2013-11-15 09:43
    Phil, thanks for the note about code boxes. I will edit my question to make it more readable and usable for future reasons.

    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):
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    GOSUB naughtySubroutine
    
    END
    naughtySubroutine:
      DEBUG "I'm a nice subroutine"
      RETURN
    
  • davejamesdavejames Posts: 4,047
    edited 2013-11-15 09:52
    Hi anticipate,

    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.
  • anticipateanticipate Posts: 10
    edited 2013-11-15 10:03
    Ok, I was confused by the fact that "subroutines" use the same syntax as other labels. It's making more sense now. For others confused by similar issues, it might be a good idea to read the language reference for:
    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.
  • davejamesdavejames Posts: 4,047
    edited 2013-11-15 10:11
    anticipate wrote: »
    With respect, davejames, I disagree. I'm coming from languages that behave differently, OOP stuff like Python, Java and C#.

    E.g. PBASIC 'uses' some comments and 'ignores' others. It could very well do the same thing with subroutines. What is the use in it EVER executing a subroutine that is never explicitly called?

    If there is a good answer, I assume it has something to do with sparing the little stamps work, since they have pretty limited resources. If that's the case, I can accept it (I just don't have the background to quite grasp why that would be).

    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.
  • davejamesdavejames Posts: 4,047
    edited 2013-11-15 10:17
    anticipate wrote: »
    EDIT: Ah, I reread your post (I'll leave my following reply intact, in case it generates something useful). Is the "subroutine" syntax the same as a region label (or whatever it's called in PBASIC)?

    Yup - you got it. :thumb: Both GOSUB and GOTO commands target labels.
    I'm willing to at least partially retract the following:
    With respect, davejames, I disagree. I'm coming from languages that behave differently, OOP stuff like Python, Java and C#.

    No prob! :cool:
  • anticipateanticipate Posts: 10
    edited 2013-11-15 10:25
    Well, what I am trying to accomplish is a little off-topic. I wrote my question this way because I didn't need help with a particular program, and I wanted it to make sense to people who read it in the future. Of course, I've made a mess of editing it, and you've all responded so quickly that there are responses to comments I've removed. So to future readers, sorry -- that's my fault.

    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. :)
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-11-15 10:33
    I pulled my previous reply. I really find PBasic a beginner's language on a small device that offers a very comfortable environment to people new to programing microcontrollers.

    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.
  • anticipateanticipate Posts: 10
    edited 2013-11-18 18:39
    I pulled my previous reply. I really find PBasic a beginner's language on a small device that offers a very comfortable environment to people new to programing microcontrollers.

    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.
Sign In or Register to comment.