Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Am I using GOSUB & Functions wrong here?

Author
Message
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 19th Aug 2011 06:03
Trying to figure out some nested menu's and I'm concerned because I have a gosub from my main loop to function 1, then within function1 one I gosub into function2.

It works; but I can call an end to the program in either function; and I'm concerned about spaghetti code problems and/or memory leaks doing it this way?



Should I be concerned or this works ?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Aug 2011 07:35 Edited at: 19th Aug 2011 07:41
Don't worry you're not using functions at all (those are called routines). After that first GOSUB you have an END. You don't want to END the program after every GOSUB because when Darkbasic sees RETURN it'll go to the next line after the GOSUB so if it sees an END the program will stop.

On Menu2 when you want to go back to Menu1 just type RETURN. Or if their within a DO/LOOP (which they should be in some kind of loop) you can use EXIT to leave the DO/LOOP and let the RETURN at the end of the routine to take you back to Menu1.

Your pseudo-code:


In this learning phase it's ok to have code that's inefficient. As you learn more and more about Darkbasic Pro you'll get better trust me. Just have fun right now and don't worry if you have few routines that seem a bit awkward.

erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 20th Aug 2011 03:08
Thanks again for the great advice Grog.

Like I said ; it works .. but I did sort of want on the radar if it was so attrocious of a practice that I should be looking to stop it immediately or just something in the long range as I get more experience I can handle better so your answer definitely lets me backshelf this for now and move on to make my program work.

Login to post a reply

Server time is: 2024-11-22 16:31:31
Your offset time is: 2024-11-22 16:31:31