Function that returns 2 results? - page 4

 

gordon It doesn't imply anything, but specifically in both C and C++ arrays are passed by reference as well (technically a pointer to the array's first element is passed, that pointer is passed by value).

   Thanks. I need to brush up my knowledge on pointer too. 

Matutin: Very interesting ! Unfortunately, EA need parameters, and sometime a lot, so we can't avoid them and have to take care for all reasons mentionned in https://www.mql4.com/go?http://www.c2.com/cgi/wiki?GlobalVariablesAreBad

 Why not write to a File & every EAs can read them. 

gordon: This discussion is about variables defined on global scope (declared outside the special function start(), init(), deinit()) and not about GV's... Don't mix the two. 

I always say global-scope variables & GlobalVariables to avoid confusion. MT4 & MT5 can do better using different terminology for these.  

 
gordon:
Why solve a problem that doesn't exist? Why reinvent the wheel? Why make up unclear code which would only work in limited cases?


Well because i wrote a function that works that way it has several moveing averages in an array 1 through 7 if any is crossed by a signal line it takes the array index*1000 + the direction ( 0 for up, 1 for down)*100 this returns a unique ID number for each order which contains three informations.

For instance an order open signal with the number 3100 is created when the third MA was crossed downwards, signal number 2000 is created if the second MA is crossed upwards.

This means my line crossing function can send that number to my open orders function, as it now contains 3 informations. firstly it itself is the magic number, secondly ordertype = signal%1000/100 result is a one or a zero decides if it should be a sell or a buy order.

so then the magicnumber contains information about which MA line was crossed to generate it in the first place. int MA=magicnumber/1000 result is a value 1 through 7 which corresponds to the original array index of the moving averages

thirdly the magic number is a unique identifier for each order and prevents multiple orders getting opened due to repeat signals as two orders with the same magic number are not allowed

later in the program if i want to perform an operation based on the type of order it is I do type=(magicnumber%1000)/100; if result is 1 it was a sell order if it is zero it was a buy order, if i want to know which MA line created that order I do int MA=magicnumber/1000 obviously the interger drops any floating point and gives me the original MA index 1 through 7

I thought this was a neat way to create more than one retrievable and usable information from a single function return.

 
SDC:

Well because [...]

That's great, but again - why not just do it the direct, universal, clear and well accepted way. Why go to all this trouble? Don't u think this is simpler:

int a,b;
void MyFunc( int& a, int& b )
   {
   //... function result should be 'passed' to a and b
 

i dont know i suppose that would have worked too, it just seemed like a good idea at the time, it was mainly because i was working on an EA tp manage several different trading strategies at the same time so i worked out that system to manage orders by magicnumbers depending on which trading criteria they belonged to so for instance you can close out all sell orders opened by a 55 periods moving average cross independantly of all other orders because 55 MA is index 3, the magic numbers of those orders would be based on 3000 then i realised i could take it a stage further and incorporate the order type too so sell orders on that MA line have magic number 3100 3101 3102 etc this means you can identify all orders opened by that trading critera, or all buy orders opened by that trading critera or all sell orders opened by any trading critera etc all by the special magic number