Returning an ARRAY from a function

 

Hello freinds,

I understand that a function can return ONE veriable.

If I want to return more then 1 veriable, (f.e. a number and a string), Can I defind within the function an aray (f.e. kuku[0,0] ), which its first
place is for the number and its second place is for the string, so... I returning 2 veriables? I hope you understand the "trick"...

Thanks for your responses.

 

you want to learn about "reference": https://www.mql5.com/en/forum/117210

 
1005phillip wrote >>

you want to learn about "reference": https://www.mql5.com/en/forum/117210


Thankes, But maybe I did not explained myself correctly.

I did not mean that I want to enter an array into the function, but the function will have a results of 2 parameters, which I put them within an array,
so the function will get 2 parameters outside of it.

I hope you understand me better, now!!
 
crossy:
I did not mean that I want to enter an array into the function, but the function will have a results of 2 parameters, which I put them within an array,
so the function will get 2 parameters outside of it.

No, an array can hold only one type of variable and that's not the way to do it anyway... You can do something like this:

void MyFunction( string& StrInput, int& IntInput )
  {
   //do something with StrInput, IntInput 
  }

//to use this function u have to pass it actual variables (trying to pass constants will not compile).

p.s. I recommend u visit that link that phillip mentioned. He understood u just fine.
 
gordon wrote >>

No, an array can hold only one type of variable and that's not the way to do it anyway... You can do something like this:


Thankes gordon, and if the two parameters are integers or strings, Can I?
 
crossy:
Thankes gordon, and if the two parameters are integers or strings, Can I?

Sure. But why do it in the first place? (unless u actually want to pass an array...)

 

Yes, I was wronge. I want to pass an array outside of the function. Is it possible?

 
crossy:

Yes, I was wronge. I want to pass an array outside of the function. Is it possible?

Like so:

void MyFunction( int& IntArray[] )
  {
   //do something with IntArray[] 
  }

//note: when u call the function u should pass the array name with no brackets (without '[]')
 
gordon wrote >>

Like so:

Thankes, but Not realy.


I want like this:

int kuku(int x, string y, int z)
{
int GGG[x];
// do someting
return(GGG[x]);
}



Am i clear now?

 
Oh brother... Did u bother reading the link phillip posted? Either u didn't read it, or didn't understand it.
Arrays can only be passed by reference in MQL4 (they cannot be passed by value; they cannot be returned using return()).

crossy:

Am i clear now?

You were clear from your first post... U just don't understand the replies.
 
crossy, you cannot use return() to return anything other than one single value.

if you want to return more than one value or an array you have to return it through the argument list. You must use the call-by-reference calling convention for the arguments you want to use for that, so your function can return their values through the argument list instead of return(). If you want to return values in an array you pass the function an empty array by-reference and the function will then be able to write its values directly into this array.
Reason: