Download MetaTrader 5

How the Program Flow?

To add comments, please log in or register
chua le
234
chua le  
//+------------------------------------------------------------------+
//|                                                 chuale.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.abc.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//---
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
  Total=OrdersTotal();
       
    if(A<B)
       {
      ......
      ...... 
      return(0);
       }
      
     if (A<C)
       {
      .........
      return(0);
       }  
   return(0);
   }

Hi,

What is the function of return(0)? When encounter, does the program go to start() or continue downward?

 Regards 

Keith Watford
Moderator
10639
Keith Watford  

No, it returns to where the function was called from.

When an expert is attached to a chart, MT4 calls init , then start, then on the return from start, it waits for the next tick and calls start again. Unless there is a reason to call deinit, where the next call will be init.

chua le
234
chua le  
1 //+------------------------------------------------------------------+
2 //|                                                 chuale.mq4 |
3 //|                        Copyright 2014, MetaQuotes Software Corp. |
4 //|                                              https://www.mql5.com |
5 //+------------------------------------------------------------------+
6 #property copyright "Copyright 2014, MetaQuotes Software Corp."
7 #property link      "http://www.abc.com"
8 #property version   "1.00"
9 #property strict
10
11 //+------------------------------------------------------------------+
12 //| Expert initialization function                                   |
13 //+------------------------------------------------------------------+
14 int init()
15  {
16
17 //---
18 //---
19   return(0);
20  }
21 //+------------------------------------------------------------------+
22 //| Expert deinitialization function                                 |
23 //+------------------------------------------------------------------+
24 int deinit()
25  {
26 //---
27
28   return(0);
29  }
30 //+------------------------------------------------------------------+
31 //| Expert tick function                                             |
32 //+------------------------------------------------------------------+
33 int start()
34  {
35  Total=OrdersTotal();
36       
37    if(A<B)
38       {
39      ......
40      ...... 
41      return(0);
42       }
43      
44     if (A<C)
45       {
46      .........
47      return(0);
48       }  
49   return(0);
50   }
so when the ea attach to chart, it execute line 14 to line19, then line 33 to line 50. My question is when it encounter line 19, line 41, line 47 and line 49, what will happen? For example, if it encounter return(0) at line 41, will it go back to line 33 or it continue with line 42, 43, 44 etc?
whroeder1
15719
whroeder1  
You need to learn to code. Your question is so basic, so generic, and is part of every programming language.

When it reaches line 19 it returns from the init() function to the caller which is the terminal. when it reaches any return statement in start, it returns to the caller which is the terminal.

int start(){
   myFunction();
   Print("returned from myFunction");
}
void myFunction(){
   Print("in myFunction");
   // return is optional for void functions, the closing brace is a return.
}
// output:
// in myFunction
// returned from myFunction
chua le
234
chua le  

Hi ,

 

I refer to mql4 manual and it did not explain clearly about return(0). Does it mean that the return(0) will pass any return value to the terminal and continue with subsequent line of coding? 

Ex Ovo Omnia
3316
Ex Ovo Omnia  
chuale:

Hi ,

 

I refer to mql4 manual and it did not explain clearly about return(0). Does it mean that the return(0) will pass any return value to the terminal and continue with subsequent line of coding? 

 

The statement stops the current function and continues by the next statement in the caller function. In this case it is inside an event handler (start()), so it returns to the event dispatcher, which you have no access to.

whroeder1
15719
whroeder1  
chuale: and continue with subsequent line of coding? 
  1. What part of " it returns to the caller " was unclear?
  2. Did you bother to look at my example and the expected output?
chua le
234
chua le  
//+------------------------------------------------------------------+
//|                                                 mql4 example.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int start()
{
   myFunction();
   Print("returned from myFunction");
}
void myFunction()
{
   Print("in myFunction");
   // return is optional for void functions, the closing brace is a return.
}

// output:
// in myFunction
// returned from myFunction
I compile it and it has error
Keith Watford
Moderator
10639
Keith Watford  
Yes, I would think that you get the error that start() must return a value
chua le
234
chua le  

Dear WHRoeder,

 

Could you please advise?

Thank you 

whroeder1
15719
whroeder1  
chuale: I compile it and it has error
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here. Same goes with "has error"
  2. It doesn't have an error, it has a warning. Now think!
12
To add comments, please log in or register