Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Codewars says "not all code paths return a value" while solving C++ problem – I wonder why?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading