# What are Function return values ? How do I use them ?

Moderator
17998
2012.05.20 13:18

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 . . .

```if ( OrderSelect(OrderPosition, SELECT_BY_POS) == true)    //  do something if the OrderSelect has worked

if ( OrderSelect(OrderPosition, SELECT_BY_POS) )           //  simpler version

if ( OrderSelect(OrderPosition, SELECT_BY_POS) == false)   //  do something if the OrderSelect did not work

if ( ! OrderSelect(OrderPosition, SELECT_BY_POS) )         //  simpler version, note the  !  meaning NOT```

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 . . .

```int TicketNumber;

TicketNumber = OrderSend( . . . . . . . . );

if( TicketNumber > 0 )
{
Print("Order placed # ", TicketNumber);
}
else
{
Print("Order Send failed, error # ", GetLastError() );
}```

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

```if( OrderSend( . . . . . . . . ) < 0 )  //  OrderSend has failed and has returned a value of  -1
{
Print("Order Send failed, error # ", GetLastError() );
}```

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 ?

2271
2012.05.20 13:50

I think we (or should Admin actually) should create "Frequently Asked Questions", so instead replying over and over again, we just simply direct others to the solution. And gives a link to MetaEditor, for example OrderSelect() is MetaEditor > Navigator Window (Ctrl + D) > Dictionary tab > Trading function > OrderSelect().

Most FAQs : "Why my EA does not send order?", "How to send order to ECN broker ?", "How to set TP and SL for 5 (or 3) digit and 4 (or 2) digit broker ?", "How to select order (OrderSelect() function that badly coded) ?", "How to compare one double to another double ?", "I try to close all my position but there is still some left opened ?", etc, etc.

Moderator
17998
2012.05.20 14:09
onewithzachy:

I think we (or should Admin actually) should create "Frequently Asked Questions", so instead replying over and over again, we just simply direct others to the solution. And gives a link to MetaEditor, for example OrderSelect() is MetaEditor > Navigator Window (Ctrl + D) > Dictionary tab > Trading function > OrderSelect().

Most FAQs : "Why my EA does not send order?", "How to send order to ECN broker ?", "How to set TP and SL for 5 (or 3) digit and 4 (or 2) digit broker ?", "How to select order (OrderSelect() function that badly coded) ?", "How to compare one double to another double ?", "I try to close all my position but there is still some left opened ?", etc, etc.

Yep I agree . . .

I have created a few of my own using my own URL shortener . . . ECN 4/5 Digits Trailing SL

1103
2012.05.20 14:18
RaptorUK:

So I thought I would make a post specifically about this subject so that I can simply link to it in the future . . . .

Some sites, like the FOREX Strategy Builder site for one, have a Karma button or a like button to show if somebody is posting helpful things or just random crap.

We don't have one here but you should consider your Karma count increased significantly for this post (and many many others) [and that goes for you too onewith :-) ]

2584
2012.05.20 14:35
Yeah thats a good post Raptor, and I agree it would be useful to have a "like" button to rate posts, and a FAQ
872
2012.08.13 17:47

I would sugest a thread similar to the book to cover these aspects of coding so we could send learners to the FAQ's to a thread of learning but given the hijack nature of threads its not going to last long as a strictly for reference thread.

5
2012.08.13 17:55
very good information. where can i buy a book?
Moderator
17998
2012.08.13 18:02
mattdillon:
very good information. where can i buy a book?