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.

DarkBASIC Discussion / I don't understand functions.

Author
Message
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 27th Feb 2005 11:02
Hello.

I have been using DB for a while now, and have learned alot, I have figured out variables, arrays, and all that good stuff, so I would say that the next big stepping stone is to learn functions. I really don't understand the concept, nor how to execute or create them. I don't understand what they are used for, or why they are used, I really know nothing about them. The only thing I do know, is that they are used frequently in programs, and that I will benefit greatly for learning them.

So I ask you, fire away, I am all ears, and ready to learn.

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
hyrichter
21
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 27th Feb 2005 11:29
I like to think of functions as "add-on" commands to the language. For example, I will write functions like MakeBullet, MakeEnemy, MakeExplosion, etc. You've been using built-in functions all along with DB. For example, newxvalue, camera angle x(), etc. These are examples of functions that return a value. Other functions include "texture object, move object, position object, etc. In your function declaration, you put "aruguments" in the parentheses for the values you want the function to process, whether it is returning a value or you are telling it where you want to position an object.

Now, let's create a really simple function. We'll call it "addNum." You give it two values, and it adds them together.



Now, anywhere in your code where you want to add two numbers, you can use your addNum function.



Of course the addNum function is really quite useless, but you could for example, create a function that finds the hypotenuse of a triangle. Here's a quick example:



And of course the famous "distance" function:


Hopefully this helps a little.



Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 27th Feb 2005 12:01
Ah... I see!

But I have one more question, can you have a funtion with more than two variables? For example, could your AddNum function have something like...

addnum(num1,num2,num3)

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
hyrichter
21
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 27th Feb 2005 12:50
Of course! You can have as many variables as you want. In languages like c++ and vb.net you can even create functions that will accept an unlimited amount of arguments passed to them. For example, you could go addnum(1,2,3,4,5,55,3,2,3,5,3) or addnum(2,4) and it would return the correct answer. I don't think you can do it with DB/DBP though, and I forgot exactly how to do it with vb.net.

dab
20
Years of Service
User Offline
Joined: 22nd Sep 2004
Location: Your Temp Folder!
Posted: 9th Mar 2005 23:28
Thanks! I learned alot also. You are a great person! Thanks Major Payn for starting this board. I havae learned alot.

___________________________________________________________
Visit my temp Page for video games!
http://www.freewebs.com/goodboys_dog
NanoBrain
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 10th Mar 2005 15:48 Edited at: 10th Mar 2005 15:49
To all,

I would like to add, that a function can accept an "unlimited" number of values(variables). But, it can only return a single value. I am just making sure that this is known to Major Payn and vgames master.

+NanoBrain+
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 10th Mar 2005 16:57
Nano,
I think that DBC has an in-built limitation of 12 arguments.
But, in 'normal' programming, the concept allows for unlimited arguments.

Jess.


Team EOD :: Programmer/All-Round Nice Guy
Aust. Convention!
AlexI
20
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 12th Mar 2005 01:22
thanks i am gonna use that distance function in my game for the ai
AlexI
20
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 12th Mar 2005 01:25
so to use it would i do

if answer= distance(my amount) then gosub ai
Los
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Location:
Posted: 12th Mar 2005 03:55 Edited at: 12th Mar 2005 03:57
well Distance# function has two arguments so you gotta put in the position of object one,position of object two. You got it right that you need answer=Distance in that line. It'll input the calculated answer in answer

Hey I've come to ask a question aswell. I while ago some moderator or nice bloke did a long tutorial on functions... and there are times when you can put functions above your main code at the top of the program. What was this use for?

**Officialy has a mental illness.
" Ahh to finish something! My boy, have you ever accomplished anything? "

Login to post a reply

Server time is: 2025-05-23 19:17:18
Your offset time is: 2025-05-23 19:17:18