Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 427

 
Artyom Trishkin:
And read how many times a static variable is initialized. And the question itself will disappear for its absurdity ;)

Then how to understand this: Local variables declared with the static keyword retain their values for the lifetime of the function. Each time the function is called again, these local variables contain the values they had in the previous call. Isn't a variable declared in a user-defined function local?

 
Alekseu Fedotov:

On the second question,

if the first tick doesn't catch, the second one will.

Returns the opening time of the bar. It will be the same throughout the bar.
 
novichok2018:

Then how to understand this: Local variables declared with the static keyword retain their values for the lifetime of the function. Each time the function is called again, these local variables contain the values they had in the previous call. Isn't a variable declared in a user-defined function a local variable?

Modifier static makes it static, and it is stored in memory for static variables, not local.
 
Artyom Trishkin:
Returns the opening time of the bar. It will be the same throughout the bar.

Got it right here. Thank you.

 
Artyom Trishkin:
The static modifier makes it static and it is already stored in memory for static variables, not local variables.

This is where I don't get it. In my opinion, it doesn't matter where it is stored, by code we reset the value of the variable to zero every time the function is accessed. What for?

 
novichok2018:

This is where I don't get it. In my opinion, it doesn't matter where it is stored, by code we reset the value of the variable to zero every time the function is accessed. What for?

For the first launch - when you declare it. After that, it is up to you to control its contents.
 
Artyom Trishkin:
For the first launch - at declaration. Further you control its contents by yourself.

However, in my opinion, this is not the case: the code is read line by line every time and every time it hits zero, and at the first run even if there is no initialization, the variable would take a zero value. So, the static datetime line New_Time=0; forces the program to do unnecessary work.

 
novichok2018:

However, in my opinion, this is not the case: the code is read line by line every time and every time it hits zero, and at the first run even if there is no initialization, the variable would take a zero value. So, the static datetime string New_Time=0; forces the program to do unnecessary work.

You are told "no", you say "yes"...
Just print the variable's value to the journal and watch. Why are you fighting when people explain the simplest things to you?
You can go on without me until you check it yourself.
 
Artyom Trishkin:
You say no, you say yes...
Just print the value of the variable into the logbook and see. Why bother when the simplest things are explained to you?
Next, without me, until you check it yourself.
You do not have to be offended. But apparently programmers read text differently. I always believed that if it says "equate to zero" in a program, the computer must equate to zero, regardless of whether at startup or at repeated reference to a line of code. And there's no need to visit any journal to prove zero initialization is unnecessary, you just need to remove it: static datetime New_Time; produces absolutely the same results as static datetime New_Time=0; I don't know how much faster the program works, I admit, it's not significant at all, but still it removes "unnecessary gesture".
 
novichok2018:
Don't be offended. But programmers obviously read text differently. I have always considered that if it says "equate to zero" in the program, the computer must equate it to zero regardless of whether at launch or at recurrent reference to a code line. And there's no need to visit any journal to prove zero initialization is unnecessary, you just need to remove it: static datetime New_Time; produces absolutely the same results as static datetime New_Time=0; I don't know how much faster the program's operation is, I admit that it's not significant at all, but still it removes "unnecessary gesture".
I'm not offended by stubbornness. Why?
And variables need to be initialised explicitly. You'll get to the importance of initialization someday. In the meantime, you can throw out everything inside curly brackets.
Reason: