invalid return of function within a loop

 

Hi guys,

I terminated a function of double type in a complex program without any return value, what is an obvious mistake. The function call ( 'test()' ) is triggered from inside a loop (while or for). The function test() doesnt return any value, so the variables a and b can never have any value. The following function 'MarketInfo()' returns a value of the same type of the incorrect function 'test()'. Starting with the second pass of the loop (i > 0), the return value of MarketInfo() is transferred to the variables of a and b. MarketInfo() is only an example, every function of double type that follows the incorrect use of test(), transfers its values to a and b. Can anybody explain this phenomen ??

output: i=0 --> a=0 and b=0

i>0 --> a=dig and b=dig

Thanks alot! Johann

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
double a,b;
for(int i = 0;i < 5;i++){
    a=test();
    b=test();
   Print(a,"  ",b);
   double dig=MarketInfo(Symbol(),MODE_DIGITS);
}

   return(0);
  }
//+------------------------------------------------------------------+
double test(){
   return;//termination of function without a return value
          //correct use --> return(...any value of double type...);
}
 
I can only guess that when a function is executed it stores the return() value in memory and that when control is passed back to the calling program, this value is retrieved. As your function returns without storing a value, the program retrieves the value from a previously called function. It's the only possibility that I can think of that makes sense.
 
Hub24:

Hi guys,

I terminated a function of double type in a complex program without any return value, what is an obvious mistake. The function call ( 'test()' ) is triggered from inside a loop (while or for). The function test() doesnt return any value, so the variables a and b can never have any value. The following function 'MarketInfo()' returns a value of the same type of the incorrect function 'test()'. Starting with the second pass of the loop (i > 0), the return value of MarketInfo() is transferred to the variables of a and b. MarketInfo() is only an example, every function of double type that follows the incorrect use of test(), transfers its values to a and b. Can anybody explain this phenomen ??

output: i=0 --> a=0 and b=0

i>0 --> a=dig and b=dig

Thanks alot! Johann

a and b are initialized with 0 when you declare them.

dig is not transferred to any other variable . . . not in the code you have shown.

 

Thank you very much for answer Raptor and GumRai,

The code produces this output. I have no explanation for that. Yes, Raptor, to value of a and b must always be = 0, but they change their values to the result of the following function with double type (function test() has double type). You can replace the MarketInfo()-call by any other embedded function of double type (e.g. iMA()), its the same. a and b get values from a complete other operation. If the variables have the same type of the "incorrect" function, they take the values when they are inside a loop and the function call comes from inside (I have tested also the types string, int and bool, its the same).

The cause is clear (invalid return from function test()), nevertheless I can't understand what`s going on here. I think it could be useful to check over this problem by Metaquotes. Possibly an error message or warning could draw attention to the user. Every function of non-void type must have a return-value!!

I hope Im not completely beside the topic.

Johann


 

Funny bug, I never mentioned noticed it. My guess is that a single buffer is used for all returning values of the same type. The compiler should have taken care of it.

 
Ovo:

Funny bug, I never mentioned it. My guess is that a single buffer is used for all returning values of the same type. The compiler should have taken care of it.


The strange thing is that the compiler will alert if return();, but return; and the compiler does not alert
 
Hub24:

Hi guys,

I terminated a function of double type in a complex program without any return value, what is an obvious mistake. The function call ( 'test()' ) is triggered from inside a loop (while or for). The function test() doesnt return any value, so the variables a and b can never have any value. The following function 'MarketInfo()' returns a value of the same type of the incorrect function 'test()'. Starting with the second pass of the loop (i > 0), the return value of MarketInfo() is transferred to the variables of a and b. MarketInfo() is only an example, every function of double type that follows the incorrect use of test(), transfers its values to a and b. Can anybody explain this phenomen ??

output: i=0 --> a=0 and b=0

i>0 --> a=dig and b=dig

Thanks alot! Johann

Firstly, it's a bug in the compiler (it can allow to use "return" when there is type for the function). You can report it to the Service Desk if you wish.

Then, functions use a data structure called a stack to pass parameter to a function and return value. As your "test" function is intended to return a value, but actually it return nothing, there is some sort of shift in the stack, who then returns a wrong value.

 
angevoyageur:

Firstly, it's a bug in the compiler (it can allow to use "return" when there is type for the function). You can report it to the Service Desk if you wish.

Then, functions use a data structure called a stack to pass parameter to a function and return value. As your "test" function is intended to return a value, but actually it return nothing, there is some sort of shift in the stack, who then returns a wrong value.


Then the same value was popped twice. I doubt this is the way how stacks work.
 

First of all - is it a feature of MQL4 language. You must specify return value for function test explicitly in order to avoid such surprises.

And second point - soon new compiler for MQL4/MQL5 will be released where you can turn in strict rules for MQL4 source code. Open in MetaEditor 5 you MQL4 code and add just one line. See also Beta Version of MetaTrader 4 IDE Including New MQL4 Compiler and Editor


Reason: