Gosub

This is a discussion on Gosub within the C++ Programming forums, part of the General Programming Boards category; In BASIC the GOSUB command will go to a bookmark thing (or in other forms of BASIC a numberline) adn ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    37

    Gosub

    In BASIC the GOSUB command will go to a bookmark thing (or in other forms of BASIC a numberline) adn then do the proccesses there and then continue a long until it hits the return command then it will go back to where you issued the GOSUB command and then it will continue from there. The purpose of ti is so that instead of having to make a version of the section of code for every place you need it.


    thnks in advanced,
    h3ckf1r3

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    What is your question? Like you are asking for the C++ equivalent?

    EDIT:
    You know, I'm guessing GOSUB stands for "go to subroutine"... which means that you are talking about calling a function in C++.
    Last edited by laserlight; 01-20-2008 at 04:24 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The equivalent would be calling a void function and using only global variables in the program?

    IMO, Gosub is one of the evil constructs of Basic. Essentially it achieves nothing else than pushing code that has side-effects on the program far out of sight. It does not work as a black box like functions normally should do, so to achieve code reuse, you'll need to examine the body of the gosub code each time you want to use it to ensure that it will do the right thing and not have unwanted side-effects.

    So there is no point in trying to find an equivalent for it in C++. There are vastly better ways to structure code.
    Last edited by anon; 01-20-2008 at 04:30 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    why is gosub so evil? It has come in handy many a time for me. Please explain.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I edited my post to give a better explanation.

    The idea is that once I have written a function and tested it to make sure it works, I won't need to look at the code any more. The only thing I need to know are what the arguments mean (as well as may-be some pre- and post-conditions, e.g is it OK to pass a NULL pointer) and I'm safe to call the function everywhere.

    With gosubs - because they rely on global variables - there is no such safety. Technically I'd need to make sure that the code would work correctly, if I copied the code from the gosub to the place from where I Gosub. Technically such a check is required every time you want to reuse the same Gosub.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    but when you goto the function after the funcion is done will it return?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be it would be better if you explained why you are asking. (Do you want to learn C++ having Basic knowledge?)

    Does the version of basic you know have functions (and subs) and make a distinction between global and function scope?

    If you want to learn C++, not all the usual Basic practices are of much value.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    No I am not trying to learn C++ of of basic principles. What I am trying to do is translate a program from basic to C++. I have the problem of the GOSUB command though.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    What I am trying to do is translate a program from basic to C++. I have the problem of the GOSUB command though.
    A "direct" translation is probably a bad idea: you might as well leave it as is instead of going through the hassle of translating to a different programming language.

    Consequently, I suggest a complete rewrite of the portion of code that uses GOSUB into C++ such that it uses functions with variables in local scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    So if I understand you correctly just copy and paste the code at the gosub and put it at the parts I need it for now? I had beent hinking of that but it would take up a lot of extra code but what the heck it might work.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In this case the equivalent is the combination of global variables + function which takes no arguments and returns void.

    Code:
    //global variables
    //considered harmful normally
    int a, b; 
    
    //a function
    void line1123()
    {
        //Gosub code goes here
    }
    
    int main()
    {
        //Gosub 1123
        line1123();
    }
    May-be there's a reason why you are doing this. C++ supports many programming paradigms, including unstructured programming (a.k.a. spaghetti code), however the minimum you should strive for - should you ever want to program in C++, not just port code from Basic - is structured procedural programming. That means, no GOTO (except perhaps very rare cases) and no GOSUB equivalents and no global variables (unless there's a good reason).
    Last edited by anon; 01-20-2008 at 05:32 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    I know so I should do like I said earlier and just put the code where needed.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code reduplication is another harmful programming practice. When it comes to writing a good C++ program (code-wise), you seem to be in a lose-lose situation anyway .
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    Well I am trying to find out these things. I understand the example you showed, isnt that a never ending loop. I just have one thing that I am unsure about, is a void only accesable from another place and not just going through like the int main?
    and if it does then if I went back to it then wouldnt it just go untile the end fo the void and then would it go back to the exact spot you left it of or would it just go until it hits int main. I will check up on this just a minute.
    Last edited by h3ckf1r3; 01-20-2008 at 12:01 PM. Reason: not finished

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by h3ckf1r3 View Post
    why is gosub so evil? It has come in handy many a time for me. Please explain.
    It might be evil, but in basic it is pretty much a necessary evil. When you don't have functions, gosub is as close as you can get.
    It's kind of like how if you don't have any forks you can use a knife instead, but it doesn't work as well, and you better be careful when you put it in your mouth.
    In this case since you have a fork ... erm, I mean ... you have functions, use them!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting MINI-BASIC in MASM over to C++?
    By Paul Panks in forum Projects and Job Recruitment
    Replies: 405
    Last Post: 07-04-2009, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21