0

I often get this error in C#: 'not all code paths return a value'. I know the main reason behind this is an if not followed by an else. But what if I do not want the compiler to do anything if the condition inside if is not satisfied? For example, in the following program of trying to find the quotient without the use of / operator:

namespace ConsoleApplication1 { class Program { public int Quotient( int dividend, int divisor) { int i, quotient = 0, remainder; i = divisor; while (i <= dividend) { i += divisor; quotient++; remainder = dividend - i; if (i == dividend || ((i < dividend) && (remainder < divisor))) { return quotient; } } } static void Main(string[] args) { Program obj = new Program(); Console.WriteLine("Enter the first number:"); int a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second number:"); int b = Convert.ToInt32(Console.ReadLine()); Program obj = new Program(); int quotient = obj.Quotient(a, b); Console.WriteLine("The quotient is " + quotient); } } } 

Here I want the compiler to return quotient if the condition (i == dividend || ((i < dividend) && (remainder < divisor))) is true. In case this condition is false, I want the flow to go back to while (i <= dividend) and increase i and quotient. But the compiler says 'not all code paths return a value'. How am I supposed to get rid of this error? What am I supposed to give as the block of else? Also, is there a try-catch method to overcome this error?

5
  • add a return 0 to the end of Quotient Commented Jun 27, 2014 at 6:35
  • Add return quotient; at the end of your method. Commented Jun 27, 2014 at 6:37
  • possible duplicate of not all code paths return a value Commented Jun 27, 2014 at 6:42
  • The compiler is telling you "You're telling me about a function that's meant to return an int - however, there are ways for me to reach the end of this method and still not know what value I'm meant to return" Commented Jun 27, 2014 at 6:42
  • E.g. imagine you call this function as Quotient(1,10);. i becomes equal to 10, it's never less than 1, the while loop never runs - what should the function return? Commented Jun 27, 2014 at 6:48

2 Answers 2

0

The reason you get that error is because if your code does not go into the if (i == dividend || ((i < dividend) && (remainder < divisor))) condition or it fails the function has nothing to return - for simplicity you could just return 0 as a default:

public int Quotient( int dividend, int divisor) { int i, quotient = 0, remainder; i = divisor; while (i <= dividend) { i += divisor; quotient++; remainder = dividend - i; if (i == dividend || ((i < dividend) && (remainder < divisor))) { return quotient; } } return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

0

The return type of Quotient is int but if the condition in while loop is not met int wont be return. Compiler ensures that function must return the int value. You only have return statement under condition and compiler assumes that condition might not met and method wont return any value and gives you error. Return some value un-conditionally from method.

public int Quotient( int dividend, int divisor) { int i, quotient = 0, remainder; i = divisor; while (i <= dividend) { i += divisor; quotient++; remainder = dividend - i; if (i == dividend || ((i < dividend) && (remainder < divisor))) { return quotient; } } return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.