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 / function?

Author
Message
t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 22nd Mar 2009 20:17
what is the use of functions? when should i use them and why?(yes, a very noobish Q.)

im new at this
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 23rd Mar 2009 00:34
not really a noob question, i didnt know either until last week.

if you know wat subroutines r (gosub) then take that idea and add variables to it. it takes watever is in he brackets, turns it into a variable, uses that variable(s) in the function, outputs a variable, then completely deletes the bracket-variable(s) from existence so as to not interfere with the main program.

another way to look at them is as a few basic commands, such as rgb(red,green,blue) or wrapvalue(angle). these are examples of functions, they input a variable, do some stuff, then output a result.

hope this helped!
Quirkyjim
15
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 23rd Mar 2009 00:59
Just remember that if you create or alter a variable INSIDE of a function, it won't be the same OUTSIDE of it. These are know as local variables. (versus global variable, which are the same throughout)

~QJ
That's what they WANT you to think...
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 23rd Mar 2009 01:24
yes, those bracket-variables as i called them only work in the function, after that their identity is forever forgotten, unless u use the function again
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 23rd Mar 2009 01:33
to add onto that.

you use a function USUALLY when you have some code that you use over and over throughout your program. because then you type it once at the bottom of your code and can call it from anywhere in the program.

To clarify Variables are not "destroyed" they just don't exist outside the function.

if you use a variable in a function the next time you call that function the variable will still have the same value (inside the function)

so lets say you were making a calculator. you could use a function to do different things. like add,subtract,divide, and multiply so lets use the add funcion as a example.



The code:

Function Add(Number1,Number2)

declares the function and says that it will take 2 parameters,
and the parameters are integers. the names you specify here are the variables the function will put these values into.

Next:

Sum = Number1 + Number2

Just the stuff the function does pretty simple lol just adding

Next:

Endfunction sum

This ends the function and returns the value of sum(Note: the value of some the next time the function is called will still be the same. but for this function that does not matter. but can cause problems with other functions.)


Ok thats how you create a function. but how do you use them?


just call there name:

Variable = Add(44,56)

would set variable equal to the sum of 44 and 56(100)


In addition to endfunction there is exitfunction. unlike endfunction the exitfunction command is optional and can be placed anywhere in the function. it can also return a value. so it is pretty much endfunction but can be repeatedly placed throughout the function.




Ok well I'm not sure if you needed that much info but just in case i wrote it hahaha


and like that1smartguy said you can look at them like the simple commands thats what they are essentialy they are your own commands that you make. hope that helps.

New Site! Check it out \/
t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 24th Mar 2009 22:00
that helped a bunch thanks i'll mess around with functions more.

im new at this

Login to post a reply

Server time is: 2024-05-18 05:31:17
Your offset time is: 2024-05-18 05:31:17