Function that returns 2 results? - page 2

 
cameofx:

What's missing from irusoh1 explanation is the variable need to be Array(s) [...]

irusoh1 is right: the variable doesn't need to be an array. For example:

int start()
{
   int returnvalue1, returnvalue2;
   returnvalue1 = Example(returnvalue2);
   MessageBox("Example 1: " + returnvalue1 + "\r\nExample 2: " + returnvalue2);
}

int Example(int & referenceparameter)
{
   referenceparameter = 2;
   return (1);
}
 
inkexit:

I need to return 2 results from one function.

If the two (or more) results are to be assigned to variables, just declare them as global and assign values to them in the function.
 
engcomp:
If the two (or more) results are to be assigned to variables, just declare them as global and assign values to them in the function.
It's considered good programming practice to minimize the number of global variables. It's better to pass some variables by reference (if more than one variable needs to be returned) than declare them on a global scope.
 
gordon:
It's considered good programming practice to minimize the number of global variables. It's better to pass some variables by reference (if more than one variable needs to be returned) than declare them on a global scope.
Thank you, Gordon, I didn't know that.
 
jjc:

irusoh1 is right: the variable doesn't need to be an array. For example:

My phrasing & concept is incorrect in answering the topic then. Still, I see the purpose of passing by reference is in essence the practical way to manipulate Array(s) by function.  

Using passed parameter as reference function on variables has only the practicality of modifying X number of variable(s), plus one variable with return

While using it for Array allows to modify X  number of Arrays in the parenthesis times Y sizes of each Array. 
   

 

Arrays ae always passed by reference. Trouble with MQL arrays is that they can't be changed inside the function (like resize) only their members can.

Makes no sense, and limits array manipulation scope.

Maybe this was fixied in MQL5?

 
irusoh1:

[...] Trouble with MQL arrays is that they can't be changed inside the function (like resize) only their members can.

You can resize an array passed by reference to a function from within the function... What do u mean?
 
[...] Trouble with MQL arrays is that they can't be changed inside the function (like resize) only their members can.

@ irusoh1 : Do you mean making the function parameters dynamic ? I don't get it either.

@ Gordon : Do you know why they say 'parameters are passed by value. Arrays can only be passed by reference'. I know how that would transpire on code but I don't get the reasoning. For instance, is there any occurence on other language where this is not the case? i.e. Arrays are passed by value or something. I wouldn't know what the opposite of that rule would be,,,

 

cameofx:

@ Gordon : Do you know why they say 'parameters are passed by value. Arrays can only be passed by reference'. I know how that would transpire on code but I don't get the reasoning. For instance, is there any occurence on other language where this is not the case? i.e. Arrays are passed by value or something. I wouldn't know what the opposite of that rule would be,,,

Here is a simplified explanation -> https://www.mql5.com/en/forum/117210. Arrays are always passed by reference in MQL4.
 

Hi all

Gordon said : It's considered good programming practice to minimize the number of global variables. It's better to pass some variables by reference (if more than one variable needs to be returned) than declare them on a global scope.

I'm sorry, but it's not a "technical" response :) Could you say why it is better to pass variable by reference instead of global variable ?

thanks

Philippe

Reason: