Questions from Beginners MQL5 MT5 MetaTrader 5 - page 497

 

Thanks, but I've read the first link before and the second one confirms the logic I used when writing the code.

However, I'm confused by this statement in the second link "... There isanother variable with the same name outside this loop... "It follows that the value of a global variable remains the same despite the fact that inside the block a new variable with the same name will be declared, but this nonsense because if you declare a new variable, the value of the former will be erased, then I do not understand, what is the point of using and storing two different variables (local and global), but with the same value?

 
-Aleks-:

Thanks, but I've read the first link before, and the second confirmed the logic I used when writing the code.

However, I'm confused by this statement from the second link "...outside this loop there is another variable with the same name... " But this is nonsense because if you declare a new variable, the value of the former will be erased, then I do not understand, what's the point of using and storing two different variables (local and global), but with the same value?

In my opinion, you are writing nonsense. If you declare a local variable inside a loop or function, the value of a global variable with the same name won't be affected, but the compiler will warn you that you declared a local variable with the same name as the existing global variable and working inside a function or code block whose scope includes the newly declared variable will not affect the value of the global variable. It warns you about this so that you understand that this is a new variable, and so that you don't expect the value of the global variable to change after you leave the scope of the block in which you declared a new variable with the same name as the existing global variable.

To put it simply: everything inside the flat applies to all rooms and is shared by all rooms (global variable). And everything that is inside a room - it only applies to the room and not visible from other rooms (it is a local variable).

Even simpler? Please:

If you mess up the air in the toilet (locally), that's where it's messed up. Meanwhile, the whole flat (globally) knows (as a result of your toilet experiments) that it's better not to go into the toilet... But the air is a concept for the whole flat, and all residents know about the spoiled air in the toilet. At the same time, only the lucky person who is in the toilet can fully experience all the delights of changing the local environment within the confined space (local influence of the value of the variable). The rest of us know about it, but don't feel it. When you leave the toilet, you lose the sensations (local values) that you experienced in the toilet and begin to breathe fresh air (get the global values of air), while knowing, like everyone else, that there is a kick in the pants (the result of the air contamination function, which affects the overall life of the entire flat)...

I hope I've made myself clear

 
Artyom Trishkin:

I think you are writing nonsense. If you declare a local variable inside a loop or function, the value of the global variable with the same name will not be overwritten, but the compiler will warn you that you declared a local variable with the same name as the existing global variable, and working inside a function or code block whose scope includes the newly declared variable will not affect the value of the global variable. It warns you about this so that you understand that this is a new variable, and so that you don't expect the value of the global variable to change after you leave the scope of the block in which you declared a new variable with the same name as the existing global variable.

In simple terms: everything inside the flat applies to all rooms and is shared by all rooms (global variable). And everything that is inside a room - it only applies to the room and not visible from other rooms (it is a local variable).

Even simpler? Please:

If you mess up the air in the toilet (locally), that's where it's messed up. Meanwhile, the whole flat (globally) knows (the result of your toilet experiments) that it's better not to go into the toilet... But the air is a concept for the whole flat, and all the occupants know about the spoiled air in the toilet. At the same time, only the lucky person who is in the toilet can fully experience all the delights of changing the local environment within the confined space (local influence of the value of the variable). The rest of us know about it, but don't feel it. When you leave the toilet, you lose the sensations (local values) that you experienced in the toilet and begin to breathe fresh air (get the global values of air), while knowing, like everyone else, that there is a kick in the pants (the result of the air contamination function, which affects the overall life of the entire flat)...

I hope I've made myself clear

Discovered the truth in your opus - thank you! It turns out that declaring a variable inside a block with the same name as the global variable leads to locking data from the global variable inside the block... And the warning about the already declared variable can't be removed, right?
 
-Aleks-:
Discovered the truth in your opus - thank you! It turns out that declaring a variable inside a block with the same name as the global variable results in blocking data from the global variable inside the block... and the warning about the already declared variable cannot be removed, right?
Right.
 
Artyom Trishkin:
void CalculateLWMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int        i,limit;
   static int weightsum=0;
   double     sum=0;
//---
   if(prev_calculated==0)
     {
      limit=MA_Period+begin;
      //--- set empty value for first limit bars
      for(i=0; i<limit; i++) LineBuffer[i]=0.0;
      //--- calculate first visible value
      double firstValue=0;
      for(int i=begin; i<limit; i++)
        {
         int k=i-begin+1;
         weightsum+=k;
         firstValue+=k*price[i];
        }
      firstValue/=(double)weightsum;
      LineBuffer[limit-1]=firstValue;
     }
   else
     {
      limit=prev_calculated-1;
     }
 
   for(i=limit;i<rates_total;i++)
     {
      sum=0;
      for(int j=0; j<MA_Period; j++) sum+=(MA_Period-j)*price[i-j];
      LineBuffer[i]=sum/weightsum;
     }
//---
  }
it doesn't say it correctly in the reference
 
-Aleks-:

Thanks, but I've read the first link before, and the second confirmed the logic I used when writing the code.

However, I'm confused by this statement from the second link "...outside this loop there is another variable with the same name... "But this nonsense because if you declare a new variable, the value of the former will be erased, then it is not clear to me, what's the point of using and storing two different variables (local and global), but with the same value?

Unfortunately it's not written correctly :(

void CalculateLWMA(int rates_total,int prev_calculated,int begin,const double &price[])
  {
   int        i,limit;
   static int weightsum=0;
   double     sum=0;
//---
   if(prev_calculated==0)
     {
      limit=MA_Period+begin;
      //--- set empty value for first limit bars
      for(i=0; i<limit; i++) LineBuffer[i]=0.0;
      //--- calculate first visible value
      double firstValue=0;
      for(int i=begin; i<limit; i++)
        {
         int k=i-begin+1;
         weightsum+=k;
         firstValue+=k*price[i];
        }
      firstValue/=(double)weightsum;
      LineBuffer[limit-1]=firstValue;
     }
   else
     {
      limit=prev_calculated-1;
     }
 
   for(i=limit;i<rates_total;i++)
     {
      sum=0;
      for(int j=0; j<MA_Period; j++) sum+=(MA_Period-j)*price[i-j];
      LineBuffer[i]=sum/weightsum;
     }
//---
  }
 
pako:

Unfortunately, it's misspelled there :(

Well, well, can you do the output in letters, because I can't recognize the code right now.
 
pako:
it doesn't say it right in the help.
Why are you giving me the code? I don't know what to look for in it... ...and I don't have a lot of time. Can you tell me what it's for and what's wrong with the help?
 
Artyom Trishkin:
Why are you giving me the code? I don't know what to look for in it... ...and I'm limited by time. Can you tell me what it's for and what the error is in the help.

is the code from the help

is "i" at the beginning of the function

then in the loop it's "i" again.

 
pako:

is the code from the help

is "i" at the beginning of the function

then in the loop it is "i" again

Well there is no error: a loop with global i (blue), and another loop with local i (red)

if(prev_calculated==0)
     {
      limit=MA_Period+begin;
      //--- set empty value for first limit bars
      for(i=0; i<limit; i++) LineBuffer[i]=0.0;
      //--- calculate first visible value
      double firstValue=0;
      for(int i=begin; i<limit; i++)
        {                           
         int k=i-begin+1;           
         weightsum+=k;              
         firstValue+=k*price[i];    
        }                           
      firstValue/=(double)weightsum;
      LineBuffer[limit-1]=firstValue;
     }
Reason: