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 Professional Discussion / Static variables in functions

Author
Message
smithoid
23
Years of Service
User Offline
Joined: 1st Feb 2003
Location:
Posted: 27th May 2003 02:01
Hi,

I hope someone can help! My understanding is that variables within functions are local to that function. Is there any way that such a variable can be static, i.e. still retain its value when the function is called again. The value seems to be lost between subsequent calls. One way round the problem is to declare the variable as global, but this is very messy.

Cheers!
Shadow Robert
23
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 27th May 2003 02:14
unfortunately a global is your own option but i tend to use them effectively

function MyFunction( )
if private<>null then goto public

private:
global private as boolean
private = 1

public:
print "wahey, i'm so frickin' bored!"
endfunction

i tend to do that alot to emulate the public/private/protected of C++ ... so if something needs to run every Nth look you just create the main boolean for private, and then you can add extra commands.

if you want to destroy them after a while you just do something like

function MyFunction()
if private<>null
if tickover<5
goto public
else
goto protected
endif
endif

private:
global private as boolean
global value2 as dword
private = 1

protected:
local value2 as dword
tick=0

public:
tick=tick+1
value2=value2*value2
endfunction

then that'd run for like 5 cycles when you can use value2 globally then after that it resets and is destoryed
crude but effective hehee

Within the Epic battle of the fates the Shadow and the Angel will meet. With it will harbinger the very fight of good vs evil!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 27th May 2003 02:24
Here's what I do when I need static data in DBPro:



If you analyse C assembly code to see how it deals with static data, you'll see it does exactly the same thing behind the scenes.

Login to post a reply

Server time is: 2026-07-12 00:07:11
Your offset time is: 2026-07-12 00:07:11