Download MetaTrader 5

Definition of "!"

To add comments, please log in or register
Keep Marcos
120
Keep Marcos  

Hello people!!

Why some programmers put the "!" before a function ??

example:

!isPositionOpen() and isPositionOpen()

What do the different "!" ??

 

thanks for help! =]

developer787
4
developer787  
bool SampleFunction()
{
    return(true);
}

// The "!" operator is used to negate the value of a function. If a function returns "true", it will make it "false" and vice-versa.
// Example

if(SampleFunction() == true)
{
    Alert("TRUE");
} else {
    Alert("FALSE");
}
// Alerts "TRUE".

if(!SampleFunction() == true)
{
    Alert("TRUE");
} else {
    Alert("FALSE");
}
// Alerts "False".


whroeder1
15572
whroeder1  
KeepMarcos: Why some programmers put the "!" before a function ??
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
developer787
4
developer787  
WHRoeder:
KeepMarcos: Why some programmers put the "!" before a function ??
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Since the OP did not know what the "!" meant, I used the example above to make the point extra clear and obvious, however, your clarification is correct. Hopefully the OP now understands what the "!" operator does and is not further confused by your clarification which does not answer OP's question. Cheers
To add comments, please log in or register