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?

Author
Message
CornStalks
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location: Utah
Posted: 20th Apr 2005 08:25
I just have been wondering lately...what are the use of functions? As far as I can tell, you might as well just write the same code in a function in the main loop. If you could explain why they are used, that would be cool
RiiDii
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 20th Apr 2005 10:04 Edited at: 20th Apr 2005 10:05
Take a look at the most recent TGC newsletter. There is a great article in there about functions.

A lot of code use in DB are functions, they are just hardcoded into the programming language. Some examples are:

Object Position X()
MouseX()
RGB()

Basically, anything with parenthesies "()" after is a function. User Defined Functions are just as useful. You can use them to plug in a couple of values and return a single result. For example:



This code returns the number in the brackets "()" divided by 2.
This is a very basic function, so it doesn't seem to useful. But, if it were more complex, calculating angles, trig, If/Then, whatever. Functions can even call other functions. Example:



This code, using the last code's "half()" function and returns whether the number is even or odd.

Why use a function over gosub/return? There are arguments that go back and forth about which is better, but I like functions best because they are neat and easy to debug. The compiler will tell you right away if you you forgot to use EndFunction or to return a value. Gosub/Return aren't tracked nearly as neatly by the compiler. If you forget to plug in a return, your code might actually compile and work for a while, but not the way you'd like it too. You could also end up using up all your system's memory.

"Droids don't rip your arms off when they lose." -H. Solo
REALITY II
Ace Of Spades
19
Years of Service
User Offline
Joined: 6th Mar 2005
Location: Across the ocean
Posted: 20th Apr 2005 10:52
functions are there so you do not have to type the same code over and over again.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Apr 2005 23:36
Quote: "Take a look at the most recent TGC newsletter. There is a great article in there about functions"


I agree (ahem!)

http://www.thegamecreators.com/data/newsletter/newsletter_issue_27.html#9

BatVink
CornStalks
20
Years of Service
User Offline
Joined: 12th Feb 2004
Location: Utah
Posted: 21st Apr 2005 01:46
thanks

Login to post a reply

Server time is: 2024-09-23 17:31:22
Your offset time is: 2024-09-23 17:31:22