What are Function return values ? How do I use them ? - page 3

 

buff_0:

qjol,

I tried your suggestion and I have the same warning: empty controlled statement found.


hops, you are right my mistake (& a big one)

if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,5),3,Violet)); Remove the semicplon
Alert("the order isn't close because Err no. ", GetLastError());
 
Wow you responded faster then I could make my edit. Thank you for the info.
 
Simon Gniadkowski:

I see many, many people posting code and asking questions which can usually be answered by simply checking the return values from a few Functions and printing out any relevant errors. So I thought I would make a post specifically about this subject so that I can simply link to it in the future . . . .

What are Function Return values ?

The information in this post is mainly talks about the trading functions (<-- this is a link, please click it !) . . . but what is written will also apply to any other mql4 function that returns a value.

A Function, just like a variable, has a type, for example, int, double, string, void, bool, any type other than void returns a value, the type of the value returned depends on the type that the function is declared as . . . . so a function declared as type bool will return a bool value, true or false

An example:

bool OrderSelect ( int index, int select, int pool=MODE_TRADES )

The OrderSelect() function is declared as type bool so it returns a bool value as either true or false


An example:

int OrderSend ( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

The OrderSend() function is declared as type int so it returns a value that is an integer, this integer is the ticket number if the OrderSend function has worked correctly, if it encountered an error the returned integer is set to -1.


How do we use these Return values ?


Where a function returns a bool, such as OrderSelect(), we can use code such as this to check if the function worked or did not work . . .


Where a function returns an int, such as OrderSend(), we can use code such as this to check that the function worked and report an error to the logs if it did not work . . .

. . . or a more concise version without using the intermediate variable TicketNumber . . .

In either version, if the OrderSend() fails for whatever reason, the error number will be printed to the log and, if running in the Strategy Tester the error will also be visible in the Journal tab, if running Demo or Live the error will be visible in the Experts tab.

When a function isn't performing as expected, for example orders are not being placed, the log or the Journal/Experts tab can be looked at and any errors will be easily seen, a quick analysis of the relevant error will then allow you to correct the issue with your code or code logic.


Link to this thread: What are Function return values ? How do I use them ?

Thank you very much for the explaination! appreciate it