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 / Two quick questios on functions and math

Author
Message
Data
20
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 28th Oct 2006 23:07
1. If I make a variable in a specific function,how can I still access this same variable with the same value as set in the function in my other functions and main loop of the program?

2.Does darkbasic have any built in function on determinign whether a number is even or odd? If so what is it? If not, how would you suggest I make my own?


Thanks in advance,
Data

Free sigs, email addresses and contests!
JimmyR.Co.Uk
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Oct 2006 01:37
Hello,

If you want to use a variable that can be used inside and outside of functions, you can use an array. When an array is defined, it is actually reserving a little chunk of memory for it to use that doesn't get erased until it is undimensioned, so it sticks around even when used by functions. You can also get fancy and use memblocks which are really just user defined arrays or structures.

Anyway, here's an example of using an array as a global variable and then changing it inside a function. Note that I don't even pass it as a value to the function, but I operate on it inside of the function and therefore it changes. Also note that the function doesn't pass it back out - but yet a(0)s value is still changed.



Here's the same code but without using an array:



Note that the global value of a is not affected by the function.

If we aren't using an array, then to change the value of a, the function has to output a value, and we have to capture that value:



Testing for odd or even is easy! Just divide by two and check for a fractional leftover.

This function will test an integer for being even or odd. a value of 0 returned means even. A value of 1 returned means odd.



Enjoy your day.
Data
20
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 29th Oct 2006 05:52
thanks I understand the answer to the first question great although a question about the second answer.

you used



and



when you call functions in the top one, why do you put variables in the brackets? What purpose does this have? (sorry noobie question I know but I just couldn't figure it otu for some reason...)

and when you begin the write out a function, the second one, why do you put variables in these brackets? Again whats the purpose?


sorry for the amount of questions,
Data

Free sigs, email addresses and contests!
JimmyR.Co.Uk
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 29th Oct 2006 08:08 Edited at: 23rd Aug 2010 01:22
Quote: "when you call functions in the top one, why do you put variables in the brackets? What purpose does this have?"


He's passing the 'value' of the variable (in this case it's a value, but you can pass strings also) into the function.

This allows you to send values/string into functions, so the function can perform some calculation using this input values (in this case it's "even_or_odd"). Once the calc is completed it then returns the result for you.


Here's another example, in this example the ADD() function takes two input param's and adds them together, putting the sum in a variable called result. Once it's done that task, it sends (returns) the 'result' variable back.




Data
20
Years of Service
User Offline
Joined: 26th Nov 2004
Location: Winnipeg,Canada
Posted: 29th Oct 2006 15:40
okay thanks now i all makes sense to me.

Free sigs, email addresses and contests!
JimmyR.Co.Uk

Login to post a reply

Server time is: 2025-05-25 22:49:15
Your offset time is: 2025-05-25 22:49:15