How to return multiple values from a function? - page 2

 

To return more than 1 value you need to pass them to the function inputs by reference (adding & after the type)

Here's an example:

void my_function(int& output_1, int& output_2)
{
        //....
        //Set values to output variables
        output_1 = 5;
        output_2 = 10;
}

//Example of function usage
int a, b;
my_function(a, b);
Print(a,":",b);
 

Functions that Return Several Values

Are there such functions?? Of course, you can write it yourself. Let us see what you can.
The simplest function returning one value:

int func()
{
   return(100);
}

And what if we need several values of different types to be returned simultaneously? Look here:

void SuperFunc(int& valueForReturn1,double& valueForReturn2,string& valueForReturn3)
{
   valueForReturn1=100;
   valueForReturn2=300.0;
   valueForReturn3="it works!!";
}

Now let us try to call this "amazing" function:

int value1=0;
double value2=0.0;
string value3="";
 
SuperFunc(value1,value2,value3);
MessageBox("value1="+value1+" value2="+value2+" value3="+value3);

The result is the following:

This function has one common difference from simple functions: when we declare it, after the argument we put the ampersand character (&). 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. Try to omit the ampersand characters in the above code and see the result:

 

You don't know how this helped me a lot.  Thank you very much.

Roberto Dasso:

Functions that Return Several Values

Are there such functions?? Of course, you can write it yourself. Let us see what you can.
The simplest function returning one value:

And what if we need several values of different types to be returned simultaneously? Look here:

Now let us try to call this "amazing" function:

The result is the following:

This function has one common difference from simple functions: when we declare it, after the argument we put the ampersand character (&). 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. Try to omit the ampersand characters in the above code and see the result:

 
Roberto Dasso #:

Functions that Return Several Values

Are there such functions?? Of course, you can write it yourself. Let us see what you can.
The simplest function returning one value:

And what if we need several values of different types to be returned simultaneously? Look here:

Now let us try to call this "amazing" function:

The result is the following:

This function has one common difference from simple functions: when we declare it, after the argument we put the ampersand character (&). 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. Try to omit the ampersand characters in the above code and see the result:

This was very helpful. So glad to learn a new concept. The & is a pointer to variable.
 

Dear Friends,


To return more values from a function is : 


Example : You can return as many values as you want from a function. What you do is assign to a string with a seperator, eg : , (coma) . So once the string is return to your main program you can break down 

the string and assign to needed variables. Below is an example :


string Info_2_Return = IntegerToString(Send_A_Sign_ToProceed) + "," + DoubleToString(val_index_Highest) + "," + DoubleToString(val_index_Lowest);

return(Info_2_Return);


 
Ahilash5 #:

Dear Friends,


To return more values from a function is : 


Example : You can return as many values as you want from a function. What you do is assign to a string with a seperator, eg : , (coma) . So once the string is return to your main program you can break down 

the string and assign to needed variables. Below is an example :


string Info_2_Return = IntegerToString(Send_A_Sign_ToProceed) + "," + DoubleToString(val_index_Highest) + "," + DoubleToString(val_index_Lowest);

return(Info_2_Return);


You've made life more difficult than necessary with that, LOL

 
QuickPip #:

Arrays can't be returned, but structures can ;-)

For that, just create an include file and in the class make the variables you want to return as public. You can always pick them up when needed.  This is because you cannot use pointers in MQL5 and 4. But with this method, they be like global variables whose values update and can be accessed any time.

Reason: