Functions that Return Several Values (MQL4)
Are there such functions?? Of course, you can write it yourself. Let us see what you can
有這樣的函數功能嗎?? 當然,你也可以自己寫。 讓我們看看怎麼做。
返回一個值的最簡單的函數:
int func() { return(100); }
如果我們需要同時返回多個不同類型的值怎麼辦? 看這裡:
void SuperFunc(int& valueForReturn1,double& valueForReturn2,string& valueForReturn3) { valueForReturn1=100; valueForReturn2=300.0; valueForReturn3="it works!!"; }
現在讓我們嘗試調用這個“驚人”的功能:
int value1=0; double value2=0.0; string value3=""; SuperFunc(value1,value2,value3); MessageBox("value1="+value1+" value2="+value2+" value3="+value3);
結果如下:
這個函數與簡單函數有一個差別是:在聲明函數時,我們在參數後面加上 & 字符(after the argument we put the ampersand character (&))。
如果您使用一個參數,您可以在函數內更改其值,並且在調用它之後,函數的結果將保留。 這種帶參數的操作稱為引用傳遞參數(parameter passing by reference)。 因此,您可以根據返回任意數量的不同類型的變量。(This function has one common difference from simple functions: when we declare it, . If you do this with an argument, you can change its value inside the function, and after calling it the result will remain. Such operation with arguments is called parameter passing by reference. Thus you can return as many variables of different types as you wish.)
若省略代碼中的 & 字符,則結果將如下:

