Page 1 of 1

does variable = NIL clean variable away?

Posted: Wed Sep 18, 2019 6:40 pm
by Bugala
I am quite often using something following:

Code: Select all

function func()
var = NIL
if varcheck = true then var = 1
return(var)
endfunction
My assumption has been that I create a "local var" that would get destroyed after returning from the function.

However, now I am wondering if I have misunderstood something.

For in case varcheck = true, then does "var" still get destroyed after being returned from function because it is local variable.

Or is it actually so that my first "var = NIL" doesn't actually do anything, but at varcheck = true situation, "var" is created as a global variable?

Re: does variable = NIL clean variable away?

Posted: Wed Sep 18, 2019 6:58 pm
by jPV
Yes, "var = NIL" does nothing (or actually clears the global "var" variable).

You must declare all local variables with the "Local" statement, otherwise they become global (with an exception of function parameters which are local too).

So, you should do it like this to get a local "var" variable:

Code: Select all

function func()
	local var
	if varcheck = true then var = 1
	return(var)
endfunction

Re: does variable = NIL clean variable away?

Posted: Wed Sep 18, 2019 10:06 pm
by airsoftsoftwair
Setting a variable to Nil also marks it as a candidate for garbage collection in the next cycle.

Re: does variable = NIL clean variable away?

Posted: Thu Sep 19, 2019 1:04 pm
by Bugala
Thanks Jpv, forgot I can do it that way, although have done so before.


But one more question. Is there possibility to do similar empty declaration of a variable when using Function?

For example:
CreateNewItem(20, 30, Global status)

Re: does variable = NIL clean variable away?

Posted: Sat Sep 21, 2019 12:56 pm
by airsoftsoftwair
Bugala wrote: Thu Sep 19, 2019 1:04 pm But one more question. Is there possibility to do similar empty declaration of a variable when using Function?

For example:
CreateNewItem(20, 30, Global status)
No, that doesn't make sense to me.