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

 

A question has arisen. What is the meaning (sacred) of using the argument by reference in the example (Fedoseyev Dm. textbook)?

bool hyperbola(double x,double &r){
if(x==0){
return(false);
}
r=1.0/x;
return(true);
}

You can do with a single argument x by declaring r in the global scope.

double r;
////
bool hyperbola(double x){
if(x==0){
return(false);
}
r=1.0/x;
return(true);
}

This is more necessary if the argument is calculated or gets a value inside the function and is passed as a result.

For example:

int FileOpenF(string File_NameArg,int &HandleArg)
  {
   HandleArg=FileOpen(File_NameArg,FILE_CSV|FILE_WRITE,";");//Открытие файла
   if(HandleArg==-1) // Неудача при открытии файла
     {
      Alert("Ошибка при открытии файла. ",File_NameArg,// Сообщение об ошибке
            "Возможно, файл занят другим приложением Handle= ",HandleArg);
     }
   return(HandleArg);
  }

Something confusing about sacred meanings))))

 

Please help me with this task - I need to know how many minute bars there are in today's day.

The task is to find out how many bars there are on the previous day of the week - if today is Tuesday, then look at last Tuesday.

At the same time, if there is no previous day of the week, say, that it was a weekend, then we look at the day before last.

How to do this?

So far the working idea is the following - we define the current day of the week, then we look for the same day of the week and use it to define the number of bars, but maybe there is something more elegant?

 
Valeriy Yastremskiy #:

That I'm confused about sacred meanings)))

Without a global variable, a function is more autonomous and therefore more versatile. For example, a function can perform similar calculations in 20 independent places in the code, and understandably, the result should not be stacked in one common global variable x. Instead of a variable, there can also be a more complex structure or class, or for example several classes and several structures, or an array, so you should not assign everything to one return value either. the return value is often used not to return a calculation, but success/failure in that calculation.

 
Nikolay Ivanov #:

Without a global variable, the function is more autonomous and therefore more versatile. For example, a function can perform similar calculations in 20 independent places in the code, and of course, the result should not be stacked in one global variable x. Instead of a variable, there can also be a more complex structure or class, or for example, several classes and several structures, so you should not put everything on one return value either.

To get it out (r) it still needs to be declared in the scope or global.

double r;
if(hyperbola(DBL_MIN,r)){
Alert("1/DBL_MIN=",r);
}
else{
Alert("Неправильный аргумент для функции hyperbola()");
}
 
Valeriy Yastremskiy #:

To get it out (r), you still need to declare it in the scope or global.

Yes, you prepare the variable in advance, globally or locally, and use it as an argument in a function. But the function itself will not be bound to these variables created for storage.

 
Nikolay Ivanov #:

Yes, you prepare a variable in advance, either globally or locally, and use it as an argument in a function. But the function itself will not be bound to these variables created for storage.

The question was not about this. Dimitri's function returns not an argument by reference. Why use an argument by reference? Yes, local areas will be able to declare a variable in their own place and get a response to the variable after the function is called.

And that's it???

 
Valeriy Yastremskiy #:

That's not what the question was about. Dimitri's function does not return an argument by reference. Why use an argument by reference? Yes, local areas will be able to declare a variable in themselves and get a response to the variable after the function is called.

And that's it???

If a variable is declared globally, there should be no variables with the same name. If a function is called from several functions, as in your example, you can forget about it and declare a variable with the same name in each function. This is convenient...

 
Valeriy Yastremskiy #:

That's not what the question was about. Dimitri's function does not return an argument by reference. Why use an argument by reference? Yes, local areas will be able to declare a variable in themselves and get a response to the variable after the function is called.

And that's it???

1. So the function internally can change and return any variable passed to it, not a specific global variable.

2. So that the function doesn't copy any variable of a large size into the function - an array, a structure, a text string...

 
Alexey Viktorov #:

If a variable is declared globally, there should be no variables with the same name. If you call a function from several functions, as in your example, you can leave it alone and declare a variable with the same name in each function. It is convenient...

I.e. it's only a question of localization of use for local areas with the same names of arguments by reference. Well not much, but there is something. Thought there was more to it.

 
JRandomTrader #:

1. So that the function internally can change and return any variable passed to it, not a specific global variable.

2. Not to copy into a function some variable of a large size - an array, a structure, a text string...

The question was about something else. In the third example, your 1st answer. 2й. I don't think it's a question of being able to change fields or entity elements. In most cases they are not constant, so the reference argument is obvious for most cases. BUT if the entity is constant, then... but the developers decided not to bother.

Reason: