If statement missing return value

First post here. Given this function:

  function Euclid_Division (A, B : in Natural) return Natural is
      X : Natural := A;
      Y : Natural := B;
      if X = Y then
      ^^^^^^^^^^^^^^^  this is where Gnat warning is
         return X;
      elsif X = 0 then
         return Y;
      elsif Y = 0 then
         return X;
      elsif X < Y then
         return Euclid_Division (X, Y mod X);
      elsif Y < X then
         return Euclid_Division (X mod Y, Y);
      end if;
   end Euclid_Division;

Gnat complain on the if statement as follows:

exercise_043.adb:22:7: warning: "return" statement missing following this statement
exercise_043.adb:22:7: warning: Program_Error may be raised at run time

So what did I do wrong? If I tell it to just ignore, it compute the GCD correctly.

This appears to be complaining about the lack of an unconditional return. The exception Program_Error is raised if a function body’s end is reached. So long as a return will always happen, it’s both fine and ignorable.

In general, the last elsif needs an else to cover all cases (from the compiler’s point of view), so the last elsif Y < X then can be replaced simply by an else.

Obviously, with Naturals, if it’s not the case that X = Y nor X < Y, the only possibility is that Y < X, so an else is enough to catch it.

It’s saying that, if none of your elsif tests are true, then you’ll fall out of the if statement and reach the end of the function without having returned a value. We can tell that that can never happen, but the compiler can’t. If you change the last elsif to an else, as suggested by rocher, the warning should go away. You could also put a raise statement after the if statement to signal when the impossible has happened.

Many thanks to the explanation.