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 / Gosub question

Author
Message
Falelorn
21
Years of Service
User Offline
Joined: 19th Jun 2003
Location: Canada
Posted: 13th Sep 2003 13:01
Uber Noob question here, BUT

Can you use to many gosubs? I know they are good, to keep the program structured, but Im curious if you can acutally use to many.

See I told you it was a UBER NooB question

http://matedit.com/forums/index.php (other great DB forums)
Current Project - Legends of the Sword - On my WWW
Check out profile for more information
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Sep 2003 13:34
No-one can tell you because it's a matter of personal opinion. I can tell you that you will almost never see a gosub in any code I release because I prefer to use functions instead - functions give you temporary variables and hides away variables from outside the function so that you cannot change them accidentally.

But as far as the compiler is concerned you can have as many as you want.
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 13th Sep 2003 14:46 Edited at: 13th Sep 2003 14:51
OOPS! thats not strictly right IanM, you can have a lot of gosubs, but bet you a fiver this runs your pc out of stack (if it compiles)

loop1:
gosub loop2
return
loop2:
gosub loop1
return

you are limited by the size of the stack, for normal code this isn`t a problem (I think the stack can hold something like 64000 odd return locations IIRC) but if you use recursion in your code you may run out of stack and crash.

Mentor.

[EDIT] actualy, just tried it and it crashes back into the editor, dunno what would happen to a free roaming .exe
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Sep 2003 16:57
*Shrug*

but you're always limited by available memory in some way ...
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 13th Sep 2003 19:45
Of course that would run the stack over - who codes like that? It's cyclical. You could crash any program in any language with code like that. What does that prove? I think the original question was: Can you have to many gosub routines? I would think NO as long as they just sit there waiting to be called to do something and then return back to the caller, which is the main loop.

sorry, but this all seems silly


-RUST-
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 13th Sep 2003 19:53 Edited at: 13th Sep 2003 20:03
would you like to read the post again?, that was just an example, I said

for normal code this isn`t a problem (I think the stack can hold something like 64000 odd return locations IIRC) but if you use recursion in your code

look up recursion, whats wrong with you?, get out the wrong side of the bed?

http://www.nist.gov/dads/HTML/recursion.html
http://personal.vsnl.com/erwin/recursion.htm
just answer a post as best you can and someone comes on all hardcase, to you.

Mentor.
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 13th Sep 2003 23:02 Edited at: 14th Sep 2003 04:51
no I didn't get out of the wrong side of the bed, I was posting withOUT my eyes fully open LOL

my mistake


-RUST-
Falelorn
21
Years of Service
User Offline
Joined: 19th Jun 2003
Location: Canada
Posted: 14th Sep 2003 02:11
Well im using both functions and gosubs, but I have alot of them atm. just wanted to make sure that to many wasnt a problem for DBPro.

http://matedit.com/forums/index.php (other great DB forums)
Current Project - Legends of the Sword - On my WWW
Check out profile for more information
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 14th Sep 2003 11:48
no problem, ,I admit that even I have been known to get a bit previous with my posts , there was no need for me to bite even, I could have let it pass, but then the voices ................

Mentor.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Sep 2003 12:29
On a lighter note...

recursive functions are actually very very useful, and it's worth getting to grips with them. A suitable example is turn-based games. You can use recursion to calculate the next best move, iterating through of all your pieces and all of their possible movements.

I have never used it, but I imagine A* methodolgy for finding routes also uses recursion.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Sep 2003 15:33
... maybe, but I can't think how

Recursion basically gives you a stack, but A* needs a priority queue or a heap so that it can continually check the cheapest route next.

Recursion is good for searching tree structures, and a few other things though (like flood-fill).
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Sep 2003 20:15 Edited at: 14th Sep 2003 20:16
Could you not use recursion like this for path finding...



More a question for my own knowledge, as I've never used A*.

Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 14th Sep 2003 20:23
Now its my turn for a noob question, aren't functions actually slower than gosubs?

I would be unstoppable if I could just get started...
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 14th Sep 2003 21:59 Edited at: 14th Sep 2003 22:13
only when you call them, since they check if they have to pass values to the function, and the difference is miniscule, unless you have lots (several thousand calls) in the main loop and you want the last ounce of speed so you can just get 30fps then the difference is not that great, it does exist though, I have altered code to pure goto`s before now to get the last drop of speed, not a gosub or function in sight, (this was mid 70`s, interpreted basic) murder to debug though and you need a duplicate of every function for every call you make from another location, modern compilers probably optimise the code heavily for things like that anyway, linear flow code was the kittys do-dahs for speed fiends at one time (so was/is? self modifying code), nowdays a C++ compiler can optimise to a level that equals hand assembled programs in most cases, and they get better by the year as more is researched on code optimiseation, C++ and all that OOP looks messy and ineficient to old hands like me, but the compiler cleans it up and converts it into sleek well structured assembler in no time, well, not if you call overnight compiles "no time" but you get the idea

Mentor.

SteveV: umm! yeah! then you run out of stack , the trouble is that the stack is not that large by modern standards, you have to remember that the modern PC is actualy an archaic 1/2mb PC running fancy memory management to make it look to the software like you have 256mb or whatever, but under all that apparently continous memory the poor old PC is still swapping pages of memory like the clappers and franticaly trying to stuff data into programs in 16k chunks, only Macs and the late lamented (by me anyway) Amiga had true continous memory up into the gigabyte range that was addressed directly, you try programming in original C in DOS (been there done that), first thing you need to learn for large programs is how to manage the memory pages, on an Amiga you could just set the stack to whatever you liked, with a max of 16k directly addressable you can`t do that on a PC.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Sep 2003 01:21
StevieVee, recursion isn't always the best way to do something. Mentor is right about the stack problem.

Beyond the cells immediately surrounding your start position, the first time you hit a cell with a recusive search is almost guaranteed to *not* be the smallest cost. So you have to hit that cell several times from each direction.

To show the problem, I have put together a small piece of code that searches a 10x10 grid for the target.

Over 700,000 checks will ensure that you find the shortest route using this method. The A* code I've put into Codebase will do that in under 30 checks.

Login to post a reply

Server time is: 2024-11-25 08:48:28
Your offset time is: 2024-11-25 08:48:28