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: When to use them, When not to use them

Author
Message
Hadou
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 4th Sep 2008 09:25
I was just wondering if anyone could give any advise regarding functions, partically within games, for speed and ease of coding.. Is there a point at which there are to many functions? that by breaking everything down into functions, you actually make the code more complex?

For example, the casting of a spell could be self contained within its self, e.g Cast_Heal where all behaviors, actions and checks are contained within...

Or using something like

FUNCTION Cast_Heal
Mana_Check()
Target_Check()
Health_Check()
Cast_Success?()
END FUNCTION

The other thing thats been puzzeling me, would be Arrays, not how to use them.. but how to effiecently use them, for example an MMORPG with 100's of times, containing 10's of variables each could not afford to create huge arrays to store and retrive data... Is there any useful solutions? that doesnt involve giving the user OpenSource Txt files? that they could perhaps edit?

Im sure im gunna give myself a few headaches over the next few days, lets hope I dont give you guys one either.
Terrorist Zero
18
Years of Service
User Offline
Joined: 29th Aug 2006
Location: Teh YouKai
Posted: 4th Sep 2008 17:59
Personally I feel that there is no limit to the amount of functions you have in your program, so long as it is still clear and easy to understand, since one of the idea's of functions is to create easy-to-read code that is useful not just to the creator but potentially to others reading the source code.

If it works, use it.

In the example you've used you could probably get away with using an array in the function and having some parameters rather than calling other functions that would generally decrease efficiency. If you think about it, calling a function requires the program to find that particular function and then solve the equation rather than directly solve the equation, therefore it costs a tiny tiny tiny bit more processing time just for the sake of keeping code neater.

As long as you think about what you're doing when creating yet another function all is well.


AMD X2 6400+ (3.2GHz) // K9A2 Platinum 790FX Mobo // 2GB Corsair RAM 1066MHz // Powercolor HD4870 512MB OC'ed
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 4th Sep 2008 18:36
I say, use functions as much as possible.

The disadvantages:
- Slightly slower than usual code, but this is negligible

The advantages:
- Modularity
- Re-usability
- Organisation
- Encapsulation
- Etc, etc, etc

Hadou
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 5th Sep 2008 06:26
Thanks alot guys, the information provided was Great.

I guess im going to have to work on my function code and making sure the right varibles get passed to the right Function and such
Hadou
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th Sep 2008 06:36
Hey again guys, rather then add another topic to the board, I thought that I would just continue using this post for now as the next question still relates to FUNCTIONS

The question regards the retrival of data from within the function using the

FUNCTION X
var$ = "5"
ENDFUNCTION var$

Then use the variable var$ within the main programme? or would you use

var=X()

Meaning that thou the variable is worked out within the function and then is returned, it would not be returned as var$ but then use the vairable under a different label?

Would this help/hinder debug? or cause any problems?

Im trying to understand the basic ideas of good programming practice to help myself and anyone that might wanna read my code.

Thanks guys
calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 14th Sep 2008 10:24
To return the string, you would use:

var = X()

You can even use:

print X()

But your function declaration is wrong. It needs to say:

function X()

rather than:

function X

Quote: "Would this help/hinder debug? or cause any problems?"


It wouldn't make the slightest difference. Imagine the functions as bubbles, they can each have their own set of variables, even if they share the names with variables in the main program. The advantage with this is you can copy a function from any program to any other program. A variable that exists in all of the bubbles is known as a global variable.

The optomist's right, The pessimist's right.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 14th Sep 2008 17:52
Calcyman,

Probably the best analogy I've ever heard in my life


Hadou,

It's technically known as scoping (basically the 'scope' of a variable is where it exists). If you declare a variable inside a function it'll automatically be 'local' to that function. If you put 'global' before the variable declaration, that variable can be accessed anywhere in the program.

Arrays on the other hand are always automatically created global for some reason, so if you want them to be local to a function you have to precede their declaration with 'local'.

Hadou
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 15th Sep 2008 14:58
Thanks again for the info guys Short and sweet as always

Login to post a reply

Server time is: 2024-11-24 20:01:10
Your offset time is: 2024-11-24 20:01:10