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 / Functions vs. Subroutines

Author
Message
Image All
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 20th Oct 2006 17:10 Edited at: 20th Oct 2006 17:12
This is actually my description rather than a question... >.>

I have a game I'm working on and it almost always uses functions. I've also had a lot of annoying little bugs due to the flow of the program. I had to mentally run through the code while looking at it, and read it like the computer would; I found that in almost every case it was because a function was done executing and then the flow returned to its call rather than where I expected it should end up, which caused a bug or two or six.

My conclusion is that, in order to have a smooth-running game without it crashing and slapping you in the face by telling you that you're trying to load an object twice, use functions and subroutines together Without functions, you can't have arguments, and without subroutines, you can't fix your bugs by altering the command flow



well actually I'm using labels to adjust the flow, not actual subroutines Meh.

indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 20th Oct 2006 17:11
stick with functions for sanity.
there was a lot of talk a while back in which was faster in the classic days.
I wouldnt know today unless i tested them for 6.2 with a sample timer test.

Zergei
19
Years of Service
User Offline
Joined: 9th Feb 2005
Location: Everywhere
Posted: 20th Oct 2006 17:24
A common mistake that peaple do with functions is that they forget that variables created within the functions are locals, unless theres a same global variable named. Also some create functions with procedures that aren't supposed to end up being a function, or at least not just in 1.

Common example is when either you need to pass like 10 values to the function, wich within it would represent the same values outside, and almost no local variables, or when all variables within the function are globals.

As far as i get to know, i create a function when i need to calculate or do something with a few values, and it's a process that is done more than once in one cycle, for example calculating the next position of an object according to its former position, direccion and speed.
Those and the object id would be the only values i would pass, then i could call that function with either the hero or enemies.

Functions shouldn't be just for 1 procedure, but try to acomplish that same procedure whenever its needed, regardless if its the hero, an enemy, a bird, boat.... even the sky if you wish....

Further on my stuff at...
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Oct 2006 18:00
You've just explained your problem in your explanation:

Quote: "a function was done executing and then the flow returned to its call rather than where I expected it should end up"


Quote: "without subroutines, you can't fix your bugs by altering the command flow "


You NEVER return anywhere from a sub or function other than where you called it from! That is an absolute no-no! By all means call a function, return and make decisions based on the outcome. But ALWAYS return from whence you came!



Dracula
18
Years of Service
User Offline
Joined: 7th Aug 2006
Location: DBP Recreation of Castle Csejthe
Posted: 20th Oct 2006 20:18
if you can make a program flow well without the use of functions, is this ok, or is it always better to use functinos?

D
H4ck1d
18
Years of Service
User Offline
Joined: 27th Dec 2005
Location: Yes
Posted: 20th Oct 2006 20:48 Edited at: 20th Oct 2006 20:48
@dracula:

Sure you can, but you're going to have one heck of a time going back through it for debugging and stuff. Functions are just generally better coding practice

-H4ck1d

jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 21st Oct 2006 04:55
Usually, when someone tells me that they have trouble with where they expect to return to after a function...they are nesting function calls very deep. You can easily cause recursion, which is not always a good thing. This is the result of not having completely defined the problem in the first place. I am not preaching here, I program by the seat of my pants, too...but experience has taught me that I tend to solve the same types of problems over and over again. So, I have developed a better sense of how complex a function should be allowed to be. You should not have to call too many of your other functions from within one of your functions. As convoluted as that sounds, it equates to the common acronym KISS - Keep It Simple, Stupid.

Subs are pretty weak in general because their use leads inevitably to spaghetti code with alot of gotos, labels, etc. That is a real mess even when things are going right. It is not robust at all, and bear in mind that you are still writing a Windows application underneath all of that nice wrapping, and Windows is absolutely event-driven. Spaghetti code does not run very well in this situation, it is procedural by nature, and does not tolerate interruptions, for example. So, you code is best written to respond to a single request....draw the current frame of your game. That should be done in the most orderly fashion you can muster, imo. One of the best things you can do to start supporting that is to create intelligent UDTs that will house the necessary information. Intelligent data is your best friend in DBPro programming.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 21st Oct 2006 05:53
Also make your functions so that they will work in any programme you may be coding., portability

Dark Physics makes any hot drink go cold.
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 22nd Oct 2006 20:59
@HowDo...yes, thanks for pointing that out. I use all locals too, so that I avoid scope errors....like this

function someFunction
local temp as integer

.
.
.
endfunction

Login to post a reply

Server time is: 2024-11-26 09:45:25
Your offset time is: 2024-11-26 09:45:25