Need to understand few basics : Data structures and Functions???

 

Hi

1. In MQ4, I understand that there is no concept of classes and structs. The only way I found for holding lot of data is arrays (buffer) or save to a file. Is there any other way?

2. It doesn’t say anywhere but I wasn’t able to return an array from a function. Is it the right behavior?

Thanks

Pankaj

 

1. No, there is not another way.

2. See https://docs.mql4.com/basis/variables/formal:

It is also possible to pass parameters by reference. In this case, modification of such parameters will be shown in the corresponding variables in the called function passed by reference. Array elements cannot be passed by reference. Parameters can be passed by reference only within one module, such possibility being not provided for libraries. In order to inform that a parameter is passed by reference, it is necessary to put the & modifier after the data type.

Example:

void func(int& x, double& y, double& z[])
{
double calculated_tp;
...
for(int i=0; i<OrdersTotal(); i++)
{
if(i==ArraySize(z)) break;
if(OrderSelect(i)==false) break;
z[i]=OrderOpenPrice();
}
x=i;
y=calculated_tp;
}

Arrays can be passed by reference, as well, all changes being shown in the source array. Unlike simple parameters, arrays can be passed by reference into library functions, as well.

Parameters passed by reference cannot be initialized with defaults.
 
Rosh:

1. No, there is not another way.

2. See https://docs.mql4.com/basis/variables/formal:

Passing parameters as references can help too. Thanks much.

 
Rosh wrote >>

1. No, there is not another way.

2. See https://docs.mql4.com/basis/variables/formal:


Rosh thank you, but maybe I wasn't so clear: I want to retrun an array from a function (not to get a array into the function).


I hope you understand me.
 
crossy:
Rosh thank you, but maybe I wasn't so clear: I want to retrun an array from a function (not to get a array into the function).

The array has been passed by reference, which means that any changes done on z[] inside the function, is done on the array passed to that function, hence the array is "returned".

 
gordon wrote >>

The array has been passed by reference, which means that any changes done on z[] inside the function, is done on the array passed to that function, hence the array is "returned".


Great topic......I have always wondered about the difference between passing parameters and passing by reference.

Just to confirm and reinforce my understanding, passing by reference has an effect similar to a static variable in that you can change the value of the parameter within a function where the variable(parameter) is normally outside the scope of the function being called?

So if I may construct a crude example :=

void No_Name_Function()
{
   int X = 1;
   Function_Change_X(X);
   ..................X now equals 2???
}
void Function_Change_X(int& New_X)
{
New_X ++;
}
 
kennyhubbard:
Just to confirm and reinforce my understanding, passing by reference has an effect similar to a static variable in that you can change the value of the parameter within a function where the variable(parameter) is normally outside the scope of the function being called?

No, static variables have nothing to do with this... Maybe u meant global variables (https://docs.mql4.com/basis/variables/global), but the last part of your statement ("...the variable(parameter) is normally outside the scope of the function being called?") is inaccurate in regard to global variables since the scope of a global variable is the entire program (including all functions). So I don't know what u meant.


Without getting into technical details about what is actually being passed, here's a simplified explanation of the differences between pass-by-value and pass-by-reference:

  • When passing a parameter by value to a function, the function receives a copy of the parameter.
  • When passing a parameter by reference to a function, the function receives the actual parameter.


p.s. Your example is fine.

 

Hi Gordon,

The simplified explanation sounds good thanks.

Sorry, but perhaps my static variable example was a bit weak. I was thinking of a static variable in the sense that it can be "seen" outside of its function(similar to a variable with a global scope).

 
kennyhubbard:

(...) static variable in the sense that it can be "seen" outside of its function(similar to a variable with a global scope).

But it can't. A static variable cannot be seen outside of it's function...

 

Ah, ok, my bad. Thanks for clearing that up for me.

 

Good all, Please I have the need to include yearly period in my indicator. How can I do this?

example:     

if(Period()<=60) period = PERIOD_D1;

if(Period()==240) period = PERIOD_W1;

if(Period()>=1440) period = PERIOD_MN1;

if(Period()>=43200) period = PERIOD_??;