if statements also preform actions in the conditions area?

 

Hey I have a quick question and i hope this makes sense, i'm new to mql5 and i'm doing a tutorial on YouTube and the person has some if statements and apparently they also preform actions in the condition area, is this true? I have never heard of this before.


   //get current tick
   if(!SymbolInfoTick(_Symbol,cT)){Print("Failed to get current symbol tick"); return;}
   

   if(CopyBuffer(handle,0,1,2,bufferMain)!=2){
      Print('Failed to get indicator values');
      return;
   }
   

The code above are obviously if statements but do they also preform the CopyBuffer task and the SymbolInfoTick tasks in the conditions area? Like, i always thought for an if statement you have your condition, and if it's met you then say do so and so task, but this apparently will do the copybuffer actions and the SymbolInfoTick actions regardless  and fill in the info and then check to see if they match certain criteria. Like for the copy buffer it looks like it just asks the question if CopyBuffer is !=2 then preform task, but at the same time does it also fill in the copy buffer info? Any explanation would be appreciated.  Sorry if this is a silly question.

 
thecerealslayer:

Hey I have a quick question and i hope this makes sense, i'm new to mql5 and i'm doing a tutorial on YouTube and the person has some if statements and apparently they also preform actions in the condition area, is this true? I have never heard of this before.


The code above are obviously if statements but do they also preform the CopyBuffer task and the SymbolInfoTick tasks in the conditions area? Like, i always thought for an if statement you have your condition, and if it's met you then say do so and so task, but this apparently will do the copybuffer actions and the SymbolInfoTick actions regardless  and fill in the info and then check to see if they match certain criteria. Like for the copy buffer it looks like it just asks the question if CopyBuffer is !=2 then preform task, but at the same time does it also fill in the copy buffer info? Any explanation would be appreciated.  Sorry if this is a silly question.

Yes SymbolInfoTick fills cT , if it fails it returns false . So the author hits 2 birds with one cat

The copy buffer returns the amount copied , it fills bufferMain , if its not 2 it returns an error .

You can also do it in your functions with the & which means you are passing a variable to the function as a reference , i.e. whatever you do to that variable in the function it will also happen in the variable you sent in . An example :

double MilkCartonLitres=2.0;
bool fillBowl(double &milkLitresInBowl){
//if milk is available 
if(MilkCartonLitres>0.0){
//you fill the bowl
milkLitresInBowl=MilkCartonLitres;
MilkCartonLitres=0.0;
return(true);//and signal there was milk
}
return(false);//or signal the carton was empty
}

//and you call it like this 
int OnInit(){
double bowlLitres=0.0;
if(fillBowl(bowLitres)){
Alert("Filled "+bowLitres+" of milk");
}else{
Alert("You are out of milk");
}
return(INIT_SUCCEEDED);
}
 
Lorentzos Roussos #:

Yes SymbolInfoTick fills cT , if it fails it returns false . So the author hits 2 birds with one cat

The copy buffer returns the amount copied , it fills bufferMain , if its not 2 it returns an error .

You can also do it in your functions with the & which means you are passing a variable to the function as a reference , i.e. whatever you do to that variable in the function it will also happen in the variable you sent in . An example :

alright, but can i only do this with only a couple built in functions such as CopyBuffer and SymbolInfoTick? Or do all built in functions or whatever they are called behave like this? 

 
thecerealslayer #:

alright, but can i only do this with only a couple built in functions such as CopyBuffer and SymbolInfoTick? Or do all built in functions or whatever they are called behave like this? 

Any function that returns a value, including functions you make, can be used in an if condition. A void function does not return a value and attempting to use it as an expression in an if condition will generate a compile error.

 
thecerealslayer #:

alright, but can i only do this with only a couple built in functions such as CopyBuffer and SymbolInfoTick? Or do all built in functions or whatever they are called behave like this? 

There are functions that don't do that as @Max0r847 said , and there are also functions with 2 call types available (or more) . One returns the value directly and the other one returns true / false and receives the "fill" variable as a parameter.

If you want this "behavior" on all of them you can wrap functions around the ones that don't have it .
Reason: