The Operator "return"

 

Hello, I need some help understanding the use of "return," or more broadly, how to terminate the current function and make the EA execute the next one. The only thing I understand about "return" is that it stops whatever function the EA is currently executing and restarts from the very beginning (eg from OnStart()). Right now the EA is being forced to execute every single function, even the ones that would not be used.

Thank you for your help.

 
jackyan22:

Hello, I need some help understanding the use of "return," or more broadly, how to terminate the current function and make the EA execute the next one. The only thing I understand about "return" is that it stops whatever function the EA is currently executing and restarts from the very beginning (eg from OnStart()). Right now the EA is being forced to execute every single function, even the ones that would not be used.

Thank you for your help.

If you don't want a Function to be used don't call it, there is nothing being forced to happen, your code calls it, change your code so the functions are NOT called.
 

Thanks for your response RaptorUK. I'm still new at mql4 so I'm not sure how I can accomplish that. Also, could you clarify if my understanding of "return" is correct? Let me explain my problem with more detail. Say I have these 3 functions, which are called in order. However, let's say that if function 1 is executed, I would like function 2 to be skipped. Right now, when 1 is executed, 2 is executed even though I know it is useless. I can (and am using) use conditions, but I would like to avoid them, especially if the condition is complex and takes time for the EA to compute.

function 1
function 2
function 3
 
What question determins if function 1 has been executed ?
 
jackyan22:

Thanks for your response RaptorUK. I'm still new at mql4 so I'm not sure how I can accomplish that. Also, could you clarify if my understanding of "return" is correct? Let me explain my problem with more detail. Say I have these 3 functions, which are called in order. However, let's say that if function 1 is executed, I would like function 2 to be skipped. Right now, when 1 is executed, 2 is executed even though I know it is useless. I can (and am using) use conditions, but I would like to avoid them, especially if the condition is complex and takes time for the EA to compute.

OK, let me see if I can clear this up for you . . .

There are 3 standard/special functions, init, deinit and start, anything else is a custom user defined function. Custom functions are declared/defined outside of the standard functions . . but are called from within the standard functions.

For example:

//  Global variables and externs

int magicNumber = 1845;

int init()
   {
   return(0);
   }
   
int deinit()
   {
   return(0);
   }   
   
int start()
   {
   //  local variables
   int index;
   double Bar1OpenPrice;
   
   // custom function calls
   function1();

   if(Bar1OpenPrice > Bid)
      index = function2();
   
   function3();
   return(0);
   }

void function1()
   {
   return;
   }

int function2()
   {
   if (Bars < 1000) return(0);

   return(Bars);
   }
   
void function3()   
   {
   return;
   }

(This code does nothing useful, it is just meant to show a concept)

You can see that function2 is only called depending on the result of a condition and when it is called it has 2 possible outcomes for what it returns depending on a condition, function1 and function3 are type void so do not return a value.

What you must not do . . . . ( well you can but you will make life hard for yourself ), is call function1 and from function1 call function2 and from function2 call function3 . . . .

Perhaps you should show your code so we can help you more specifically ?

 

Hmm I'm still rather confused. I do understand that you are using "return" to obtain the result of function 2. However I still don't understand how I can "skip" functions, unless I use conditions, like you just did. In your example, that would be ( Bar10OpenPrice>Bid).

I don't want to bother you with 500 lines of code, so here's a simple illustration of my problem. Pretend that the Printing functions are functions that's true/false.

In the following code, the EA never reaches the "end of EA" part. It simply loops from 1 to 4. What I would like to do is to skip 5 (and it's condition check), and get to the last like of the EA. However, I am unable to do it without recoding everything using conditions.

int start()
  {
//----
   Print("1");
     {
      Print("2");
        {
         Print("3");
         Print("4"); // << many functions go between these 2 brackets
          {
           return;
          }
         if(Print("4")==false
          {
           Print("5");
          }
       }  
       }
    Print("end of EA");
      
     
      
//----
   return(0);
  }
 

Not check for errors

int start()
{
int MyVolume ;
bool Done ;

MyVolume=functionname( 10 ) ; //after calling and executing functionname the returned value is placed into MyVolume

if (MyVolume>10000) 
  { doanotherfunction() ; }
else
  { Done=functionsmallbool() ; }
return(0) ;
}
//+------------------------------------------------------------------+ 
//Functions to use in start() section of code
int functionname( int barvalue) { //start of code

int result ; //value to return to main controling section i.e start()

result= Volume[barvalue] ; //Get the value using the passed parameter barvalue

return(result) ;
}
//+------------------------------------------------------------------+ 
 

Further explanation found in the book

custom functions here

 

Hi Ickyrus. I am basically doing what you are doing, using conditions. In the end it's just about simplicity. Right now I have to fill many lines with these condition checks, and I hope somehow there is an easier way to skip a function based on the execution of a previous function.


On a side note, could someone explain 'A return operator terminates the current function execution and returns the control to the calling program?' Does it terminate the functions of the same level, or a level higher? and what is the calling program? Does it run the calling program again and inevitably hits the return operator once again in a continuous loop?


Thanks for all the help.

 

Ther return comand or operator can be placed in a function many times but when met in the execution path i.e if ( Ask>price) return: else continue ; only if Ask has a bigger value than price will the return comand be executed stopping the function from doing any further caluculation but only if the Ask price is bigger that price, at a later point in time another return can be written in the same function to end its execution. If you are fimiliar with the idea of sub-routine GOSUB in BASIC then you can think of a function as a subroutine.

You will also need to understand scope of variable.

The chart you place the EA on calls/interprits the instructions in the EA when the chart receives new market price information(a tick). Memory is reserved for values as and when needed and destroyed when the chart finishes intrpriting your code, with the exception of static variables. The chart interprits a function call as a place to do temporary calculation and destroys (relases memory) of the temporary calculation when it mets the return command leaving it a pre-defined value to be used in the section of code that called the function.

 
jackyan22:

Hmm I'm still rather confused. I do understand that you are using "return" to obtain the result of function 2. However I still don't understand how I can "skip" functions, unless I use conditions, like you just did.

You have no need to skip a function, just don't call it. Think about it the other way round . . . stop thinking that mql4 doesn't have functions, you need to adjust the way you are thinking. Don't think "when do I need to skip this function" . . . think "when do I need to call this function".

You can use the returned value from one function to determine if you call a subsequent function. If function_a is type bool it can very easily be used to determine if function_b is called . . for example.

start()
   {
   
   if( function_a() )
      function_b();

   return(0);
   }

bool function_a
   {
   return(true);
   }

void function_b()
   {
   return;
   }
Reason: