Value of Local Variable at start of Macro.

For general Flowcode discussion that does not belong in the other sections.
Post Reply
jay_dee
Posts: 130
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 46 times
Been thanked: 29 times

Value of Local Variable at start of Macro.

Post by jay_dee »

Hi,
In a user macro, I'm aware any local variable is not retained after the macro exits.
However each time a macro start, can I always guarentee the local values will start with a Value of 0? and is the bahaviour the same between FC and FAD.
or could it be some random value.

If a local value is created with a default value, is this used as the starting value?
Or should I always explicitly set the values at the start of the macro.

Tiying up the tattered edges of my knowledge!
Thanks, J.

medelec35
Matrix Staff
Posts: 1432
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 506 times
Been thanked: 469 times

Re: Value of Local Variable at start of Macro.

Post by medelec35 »

Hi J.
If uninitialised, A local variable will be automatically initialised at 0.
Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Value of Local Variable at start of Macro.

Post by mnfisher »

To be sure - if you need 0 then set an initial value of 0.. I don't think any value is 'guaranteed' otherwise. Local variables are usually allocated on the stack by moving the stack pointer and values will depend on what is in the memory at the time...

medelec35
Matrix Staff
Posts: 1432
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 506 times
Been thanked: 469 times

Re: Value of Local Variable at start of Macro.

Post by medelec35 »

mnfisher has a valid point, thanks Martin.
I have tested for uninitialised local on an Uno and it is 0 every time after power-up.
Of course, this can't be guaranteed.
So it is best to initialise in practice.
Martin

Steve-Matrix
Matrix Staff
Posts: 1234
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Value of Local Variable at start of Macro.

Post by Steve-Matrix »

I think in Flowcode simulation and deployed Flowcode App Developer projects, uninitialised numbers will be zero and uninitialised strings will be empty.

For Flowcode projects downloaded to a chip, uninitialised variables could potentially be anything.

However, I will echo what others have said: it is always good practice to initialise variables if you require or expect them to have a certain value.

jay_dee
Posts: 130
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 46 times
Been thanked: 29 times

Re: Value of Local Variable at start of Macro.

Post by jay_dee »

Thanks guys.
For embedded (and general good practice) I shall expicitly set/reset the value at the start of the macro, since there is no guarentee.
J.

BenR
Matrix Staff
Posts: 1707
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 435 times
Been thanked: 598 times

Re: Value of Local Variable at start of Macro.

Post by BenR »

A rule of thumb with variables is always write before you read :D

I think the very strict ESP32 compiler will generate an error if it thinks you are trying to do a read before a write.

Post Reply