# Returning an array in function

Hi, So I got this code in which I calculate the intersection of 2 lines, the algorithm works but there is an error I can't solve.

Error:

``````error: expected unqualified-id before '[' token
``````

Here is the code:

``````double[] Intersect(double[2] ClineP1, double[2] ClineP2, double[2] OlineP1, double[2] OlineP2)
//the line above is where the error pops up
{
//Line1
double A1 = ClineP2[1] - ClineP1[1];
double B1 = ClineP2[0] - ClineP1[0];
double C1 = A1*ClineP1[0] + B1*ClineP1[1];

//Line2
double A2 = OlineP2[1] - OlineP1[1];
double B2 = OlineP2[0] - OlineP1[0];
double C2 = A2 * OlineP1[0] + B2 * OlineP1[1];
double delta = A1*B2 - A2*B1;
if(delta == 0 || ) {
Serial.println("Lines are parallel");}

else{

double x = (B2*C1 - B1*C2)/delta;
double y = (A1*C2 - A2*C1)/delta;
double[2] xy = {x, y};
return xy;

}
}
``````

Thank you in advance!

You can only return one value from a function. Just one. You cannot return a whole array.

You should make the array global and then there will be no reason to need to return it.

Alternatively you can return a pointer to the array, but be careful that the pointer will still be good when you go to use it.

I knew there is the 'sstream' in c++ that I used before, is there anything similar to that in arduino?

You can return a struct from a function.

You can also do this. http://cpp.sh/3ncd

Before & After: http://cpp.sh/2ecc

Using a struct: http://cpp.sh/6hct

Returning large amounts of data from a function is not a good idea because it puts it on the stack. Better to create a global array and use that.

I have made it a global array and there is still an error that pops up:

Error:

``````Final:273: error: expected ',' or '...' before 'ClineP1'
Final.ino: In function 'void Intersect(double*)':
Final:276: error: 'ClineP2' was not declared in this scope
Final:276: error: 'ClineP1' was not declared in this scope
Final:277: error: expected unqualified-id before numeric constant
Final:281: error: 'OlineP2' was not declared in this scope
Final:281: error: 'OlineP1' was not declared in this scope
``````

My code :

``````void Intersect(double[2] ClineP1, double[2] ClineP2, double[2] OlineP1, double[2] OlineP2) //273
{
//Line1
double A1 = ClineP2[1] - ClineP1[1];
double B1 = ClineP2[0] - ClineP1[0];
double C1 = A1*ClineP1[0] + B1*ClineP1[1];

//Line2
double A2 = OlineP2[1] - OlineP1[1];
double B2 = OlineP2[0] - OlineP1[0];
double C2 = (A2 * OlineP1[0]) + (B2 * OlineP1[1]);
double delta = A1*B2 - A2*B1;
if(delta == 0  ) {
Serial.println("Lines are parallel");}

else{

x = (B2*C1 - B1*C2)/delta;
y = (A1*C2 - A2*C1)/delta;

}
}
``````

Thank you in advance!

The function arguments should not specify the length of the arrays:

void Intersect(double[] ClineP1, double[] ClineP2, double[] OlineP1, double[] OlineP2)

Of course, it would help a lot if you told us WHAT the error was, not just where it is....

Regards, Ray L.

I tried removing the arguments but the error still pops up and I don't know what the error is.

What's this?

``````        if(delta == 0 || ) {
``````

I tried removing the arguments but the error still pops up and I don't know what the error is.

What is your revised code and what is the error? We don't know what the error is either.

Alright, I managed to narrow error down to 1, All i had to do was to remove the [] and put it after the variable.

My code now:

``````void Intersect(double ClineP1[], double ClineP2[], double OlineP1[], double OlineP2[])
{
//Line1
double A1 = ClineP2[1] - ClineP1[1];
double B1 = ClineP2[0] - ClineP1[0]; // this line is where the error
double C1 = A1*ClineP1[0] + B1*ClineP1[1];

//Line2
double A2 = OlineP2[1] - OlineP1[1];
double B2 = OlineP2[0] - OlineP1[0];
double C2 = (A2 * OlineP1[0]) + (B2 * OlineP1[1]);
double delta = A1*B2 - A2*B1;
if(delta == 0  ) {
Serial.println("Lines are parallel");}

else{

x = (B2*C1 - B1*C2)/delta;
y = (A1*C2 - A2*C1)/delta;

}
}
``````

Error: Final:277: error: expected unqualified-id before numeric constant

So I got this code in which I calculate the intersection of 2 lines, the algorithm works ...

So what is the problem if it works?

Is this related to http://forum.arduino.cc/index.php?topic=307012

So this is a slope of two line sand I want to know the heading of each line to know where they intersect.

I managed to narrow error down to 1

You chose to ignore reply #9, right?

Why post, if you ignore the answers? May as well get a Ouija board.

a few thoughts

double delta = A1*B2 - A2*B1; if(delta == 0 )

comparing a double to 0 is asking for trouble

in arduino ide 1.62 a double named B1 won't compile

the resulting sign of your intersect point is incorrect when I tried

double l11[] = {0,0}; double l12[] = {3,3}; double l21[] = {0,3}; double l22[] = {3,0};

Intersect( l11,l12,l21,l22);

racpi: a few thoughts

double delta = A1*B2 - A2*B1; if(delta == 0 )

comparing a double to 0 is asking for trouble

in arduino ide 1.62 a double named B1 won't compile

the resulting sign of your intersect point is incorrect when I tried

double l11[] = {0,0}; double l12[] = {3,3}; double l21[] = {0,3}; double l22[] = {3,0};

Intersect( l11,l12,l21,l22);

Once I changed the name of B1, the function worked. Thank you!