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.
Value of Local Variable at start of Macro.
-
- Posts: 198
- http://meble-kuchenne.info.pl
- Joined: Sun Dec 20, 2020 6:06 pm
- Has thanked: 76 times
- Been thanked: 54 times
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 713 times
Re: Value of Local Variable at start of Macro.
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...
-
- Matrix Staff
- Posts: 1919
- Joined: Wed Dec 02, 2020 11:07 pm
- Has thanked: 622 times
- Been thanked: 645 times
Re: Value of Local Variable at start of Macro.
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.
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
-
- Matrix Staff
- Posts: 1471
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 204 times
- Been thanked: 348 times
Re: Value of Local Variable at start of Macro.
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.
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.
-
- Matrix Staff
- Posts: 1926
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 503 times
- Been thanked: 686 times
Re: Value of Local Variable at start of Macro.
A rule of thumb with variables is always write before you read 
I think the very strict ESP32 compiler will generate an error if it thinks you are trying to do a read before a write.

I think the very strict ESP32 compiler will generate an error if it thinks you are trying to do a read before a write.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel