(small) bug: function signature in mqh file not matching the param list in mq4, yet compiles and runs (incorrectly)

 
not the worst bug in the world but still a bug is a bug :)

Consider a function like this, residing in the mq4 file of a library:
#property library
 
// #include <CommonLib.mqh>
 
string PeriodToStr( int iPeriod )
{
    switch( iPeriod )
    {
        case PERIOD_M1: return ("1 minute");
        case PERIOD_M5: return ("5 minute");
        case PERIOD_M15: return ("15 minute"); 
        case PERIOD_M30: return ("30 minute"); 
        case PERIOD_H1: return ("1 hour"); 
        case PERIOD_H4: return ("4 hour"); 
        case PERIOD_D1: return ("Daily"); 
        case PERIOD_W1: return ("Weekly"); 
        case PERIOD_MN1: return ("Monthly"); 
    }
}

and the following declaration in the header:

#import "CommonLib.ex4"
...
//+------------------------------------------------------------------+
// "1 minute", "5 minute"...
string PeriodToStr();

This is declaration is in fact wrong, because the definition of the function requires 1 int param (iPeriod). However the library files compile without errors and it is possible to call the PeriodToStr function from an expert. You need to call it with no parameters (as it was declared in the header. And which is wrong). The call does not generate any errors but returns an empty string regardless of period.

In my opinion this is wrong - I would expect, when compiling the expert to get an error saying that the function
string PeriodToStr(void)
does not exist.

In practice, the programmers could overcome this problem by including the library header in the mq4 files of their libraries (I commented it out above, to illustrate the error). If the header is included into the library then a compilation error happens when compiling the library mq4 file, which is the correct behaviour. However it is not necessary for this library (and most other libraries) to include it's headers.

I think the correct behaviour, even if the header is not included, is to fail when linking the expert with the library (or when compiling, if there is no concept of linking in MQL)

cheers
4x4ever
 
The compiler does not check correctness of the announcement of function from library. You too much and quickly write also to me difficultly to be in time for you.
 
You should write some function(s) declaration(s) after #import "CommonLib.ex4"

Please use right import declaration.
 
oookkkkeey... I think I begin to understand what is happening here and why I have been having problems with putting my functions into libraries...

So, yes it was my mistake - I did not include explicit function declarations in my expert file, immediately after the #import directives, as the MQL help specifies:

For compiler to be able to form the imported function call and pass parameters in a proper way, the full description of functions is needed. Functions descriptions follow the #import "module name" immediately."


#import "stdlib.ex4"
   string ErrorDescription(int error_code);
   int    RGB(int red_value, int green_value, int blue_value);
   bool   CompareDoubles(double number1, double number2);
   string DoubleToStrMorePrecision(double number, int precision);
   string IntegerToHexString(int integer_number);
#import

Stringo, the only thing I can say in my defense is that the syntax of MQL resembles C so much that it made me think "in C" :) I thought that because I have the proper #include and the proper #import directives everything should work - the include tells the compiler what are the signatures of the functions in the library and the #import tells the run-time environment where evryting is. That's what I thought. In fact, as I understand now, after reading the help again, you need to declare the functions that come from your ex4 module once again, in your main agent file, immediately after the #import directive. I don't think I like this solution (certainly defeats the purpose of having mqh files, in my opinion), but least now I understand what is going on and why functions which I put into libraries suddenly mysteriously "stop working".

Thank you very much stringo. :)
In the future you can just say "RTFM: Importing of functions!" and save yourself some time (if you don't know, RTFM means "Read The F***ing Manual - a favourite expression of Unix sysadmins from the 70's :)

Oh I am so happy - I should be able to put my functions back into the libraries and specify the #imports properly and everything should work :) If it doesn't you'll all hear from me :)

I have to admit that even though I don't like certain design and implementtion decisions of MT4 and MQL - the staff of MetqQuotes Software, like Rosh, stringo (don't know whether irusoh1 is also from MQS?) are great guys and do an excellent job of answering people's questions and providing information and resources to read about all problems.

Spasibo!
 
By the way - in light of this discussion: shouldn't you fix your file stdlib.mqh? It looks like this right now:

//+------------------------------------------------------------------+
//|                                                       stdlib.mqh |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#import "stdlib.ex4"
 
string ErrorDescription(int error_code);
int    RGB(int red_value,int green_value,int blue_value);
bool   CompareDoubles(double number1,double number2);
string DoubleToStrMorePrecision(double number,int precision);
string IntegerToHexString(int integer_number);
I think there should be one more #import at the very end - to conform to the format specified in the MQL help:

#import "<library name>"
<declarations>;
#import
Reason: