Page 1 of 1
Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 9:13 am
by AZJIO
Is the variable equal to zero when declared
Code: Select all
Global q; not explicitly specified
Global w = 0; set explicitly
If I need a variable to be equal to zero, should I explicitly set this or is the variable initially equal to 0
Re: Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 9:19 am
by STARGĂ…TE
AZJIO wrote: Tue Sep 27, 2022 9:13 am
Is the variable equal to zero when declared
Yes, integers are 0, floats are 0.0 and strings are #Null$ (not #Empty$).
There is no undefined state for these variables when no value is defined, like in other languages.
Re: Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 9:25 am
by mk-soft
Variables are initialised with 'NULL'. String variables are initialised with 'Nothing' (NULL pointer).
Re: Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 9:37 am
by AZJIO
The main thought is, can I do this:
Code: Select all
Global s$
If s$ = ""
Debug s$
EndIf
s$ + "h"
Debug s$
Global q
If q = 0
Debug q
EndIf
q+5
Debug q
that is, to use variables that are not explicitly set, as if they are set.
As well as other types of variables, but not the list array and map.
On any operating system, passing a variable to WinAPI functions as a pointerr (@s$ or @q)
Re: Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 12:58 pm
by BarryG
AZJIO wrote:Is the variable equal to zero when declared
It's mentioned in the Manual:
https://www.purebasic.com/documentation ... ables.html
Where it says:
"If you don't assign an initial value to the variable, their value will be 0."
And you can test it:
Code: Select all
Global q; not explicitly specified
Debug q ; Shows 0
So yes, you can do what you're asking. I do that all the time.
Re: Is the variable equal to zero when declared
Posted: Tue Sep 27, 2022 4:56 pm
by #NULL
Just one more note: zero-initialization will not happen if the variable has been declared already. So make sure you know your code
Code: Select all
Global a
Debug a ; 0
a = 123
; ...
; ...
; ...
Global a ; (not zero-initialized)
Debug a ; 123