- Home /
The question is answered, right answer was accepted
error "not all code paths return a value" in a function
Sorry, because I know this has been asked a hundred times before. But I have no idea why this code is giving me this error, because I cant see any path it could take for it to not return a value...
Vector3 roadSnapping (Vector3 point){
//goes through every road in the array, checks if snapping to end point
for (int i = 0; i < controlPoints.GetLength (0); i++) {
if (Vector3.Distance (controlPoints [i, 0], point) < 10) {
return controlPoints [i, 0];
} else {
return point;
}
}
}
The "for" loop is unavoidable, it will always go through this, unless the length is less than 1, however if I put a "return point;" after the loop, I get an error message calling it unreachable code. thanks for any help u can give :)
ok so I even tried this...
Vector3 roadSnapping (Vector3 point){
//goes through every road in the array, checks if snapping to end point
for (int i = 0; i < controlPoints.GetLength (0); i++) {
if (Vector3.Distance (controlPoints [i, 0], point) < 10) {
return controlPoints [i, 0];
} else
return point;
}
if (controlPoints.GetLength (0) == 0) {
return point;
}
}
now even the for loop not getting iterated is accounted for.... I'm lost...
You have to understand how a compiler actually deter$$anonymous$$es a condition. "GetLength" is a method which returns a dynamical value. The compile can't tell whats the valid range that method might return at runtime. Even such a case would produce the error:
// this will produce an "not all code paths return a value" error
public int Test()
{
bool b = true;
if (b)
return 3;
}
"b" is a variable and it can be either true or false. "We" know that b is always true, but the compiler can't "think" and doesn't assume anything. The only thing that would work is a constant value:
// this is valid as "true" will be always true
public int Test()
{
if (true)
return 3;
}
// this works as well as b is a constant here and not a variable.
const bool b = true;
public int Test()
{
if (b)
return 3;
}
Answer by Bunny83 · Apr 04, 2016 at 11:29 PM
As you said yourself the for loop only enters if the length of your array is greater than 0. Actually it doesn't matter what condition you use in the for loop unless it's a constant expression which can be determined at compile time which isn't the case here. A method that has a return value must always return a value. So you can't just leave the method without a "return" statement.
So you have to have a return after the for loop. I doubt that you get an "unreachable code" error unless you do something else wrong. This should work:
Vector3 roadSnapping (Vector3 point)
{
for (int i = 0; i < controlPoints.GetLength (0); i++) {
if (Vector3.Distance (controlPoints [i, 0], point) < 10)
{
return controlPoints [i, 0];
}
}
return point;
}
The else in your code doesn't make much sense as you would always exit the for loop in the first iteration so the for loop would be pointless.
Note: My example will return the first point which is closer than 10 units to the test point. Keep in mind that doesn't have to be the closest point. We don't know what you actually want to do so we can't suggest a specific solution.
ok, ill go try it again... functions give me a headache haha
right so I tried it again, and I don't know why it gives the unreachable code error (its a yellow one, not really an error.) well it points to the for loop, not even the return, so ill just ignore it. thanks, works I guess ;)
Well, if the compiler shows an "unreachable code" warning that that code is indeed unreachable. $$anonymous$$y example code doesn't produce that error, so you might have something else in your code that you didn't include in your question. For example an unconditional return before the loop or an additional for loop condition which always evaluates to false (at compile time).
unreachable code is pointless code. That means the compile can savely remove it as it doesn't make a difference if it's there or not.
its pointing right at the for loop. theres no other code that would conflict with that for loop in any way so I have no idea why its doing that... ill have another look at get back to you