I was trying to solve a problem in Codewars 8 kyu and when I’m done writing a solution it says:
not all code paths return a value
This is my code
using System;
public static class Kata
{
public static int Quadrant(int x, int y)
{
if(x > 0 && y > 0) return 1;
else if(x < 0 && y > 0) return 2;
else if(x < 0 && y < 0) return 3;
else if(x > 0 && y < 0) return 4;
//else return 0; // works very fine with this condition
}
}
/*
[TestCase(1, 2, 1)] return 1
[TestCase(3, 5, 1)] return 1
[TestCase(-10, 100, 2)] return 2
[TestCase(-1, -9, 3)] return 3
[TestCase(19, -56, 4)] return 4
[TestCase(1, 1, 1)] return 1
[TestCase(-60, -12, 3)] return 3
*/
Can someone help me to understand why it’s not working?
I tried to google why it’s not working and I read that the function has to return something but the compiler found a way to reach the end of the function without returning anything.
>Solution :
What happens if x or y are 0? Then none of your conditions are met, and no value is returned from Kata.Quadrant.
If you want to avoid this, ensure you either use else or have an unconditional return at the end of your method.