Variable and Function Calls?

 

Hi,


when you call a function that returns a value and assign it to a variable...and then use that variable in your code. Does the function get executed each time (the variable just makes the code look nicer)? Or does the function get executed only once, i.e. at the variable assignment, and then each time you use the variable the program just retrieves a value from the memory (the variable reduces the number of function calls needed)?


Let me give you an example:

int iA;
iA = OrdersTotal();//--------------------------------OrdersTotal() called once. Let's say the function returns 5 orders.

if(iA>0)//-------------------------------------------OrdersTotal() called second time? Is this line the same to "if(OrdersTotal()<1)"?
{       //-------------------------------------------Or does the program *already* know iA=5 and *only* pulls the result from memory
        //-------------------------------------------without executing OrdersTotal() again?

   for(int i=0;i<iA;i++)//---------------------------Is OrdersTotal() called the third time? Or is the value of iA pulled from memory?
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY) return(0);
   }
}

I hope you understand what I wish to know. My question is very basic in nature but I want to know the answer.

Thanks in advance. :)

 
Roost_12:

[...] I hope you understand what I wish to know. My question is very basic in nature but I want to know the answer.

Thanks in advance. :)

OrderTotal() is called only once in your example. In other words, if you did a long sleep immediately after iA = OrdersTotal(); then the value in iA might no longer match the number of open orders (because of manual activity, or activity in other EAs).