First of all, forgive my lack of knowledge, I am fairly new to programming but I have a high level of interest in the subject. We were working on a bit of code in class yesterday and we even stumped our teacher, who then took most of the class trying variations of this code. We were attempting to write a program that would determine if input was a palindrome. The only way I could think to do it was as follows, but I kept getting the error message in the title:

class Program
   {
      public static bool isPalindrome(int first, string word, int last)
      {
         if (word[first] != word[last])
         {
            return false;
         }
        
         else if (first < last)
         {
            isPalindrome(first + 1, word, last - 1);
         }

         else
            return true;        
      }

      static void Main(string[] args)
      {
         string word;
         Console.WriteLine("Enter a word to test: ");
         word = Console.ReadLine();

         int last = word.Length - 1;

         bool palindromeTest = isPalindrome(0, word, last);

         if (palindromeTest == true)
         {
            Console.WriteLine("Yes the word is a palindrome.");
         }
         else
            Console.WriteLine("No, that word is not a palindrome");


      }
   }

I guess my question is- in what scenario would a call of the method not return a value? Thanks for any help.

All code-paths must return a value means that if you put an if statement in, as you have, every single one must return the return type.

In your example, you use if else if else. Your else if does not return a value, thus not all code paths are returning a value. Even if the statement is returning into itself, it still must return a value, in this case a bool.

Thanks for the response. I understand your explanation, I think I am stuck then. The problem I am running into is that I need a way to recall the method with the new variables without returning a bool value to main. I have tried many variations but I am at a loss as to how I can achieve this without getting the error. One variation I just tried gives me the same result, assumably from the same issue just in a different place.

public static bool isPalindrome(int first, string word, int last)
      {
         if (word[first] != word[last])
         {
            return false;
         }
        
         if (first > last)
         {
            return true;
         }

         isPalindrome(first + 1, word, last - 1);
                
      }

The way the computer views the situation is that there is a very real possibility that the arguments within those if's could result in false, which would mean that neither if would be entered and it would not be able to return a value.

There is a possible fix to this that I can see (there is probably a better way but I'm no master) based on your first piece of code:

public static bool isPalindrome(int first, string word, int last)
      {
         if (word[first] != word[last])
         {
            return false;
         }
 
         else if (first < last)
         {
            if(word[first+1] != word[last-1]){
              return false;
            else
              return true;
         }
 
         else
            return true;        
      }

That way instead of trying to reiterate the process the reiteration is done manually and from within the method so it can still return a bool value. (You'll probably need to edit that to match the arguments and return conditions required.)

[Edit]
To base it off your most recent piece you could also do something similar to this

public static bool isPalindrome(int first, string word, int last)
      {
         if (word[first] != word[last])
         {
            return false;
         }
 
         if (first > last)
         {
            return true;
         }
 
         return isPalindrome(first + 1, word, last - 1);
 
      }

Or even

public static bool isPalindrome(int first, string word, int last)
      {
         if (word[first] != word[last])
         {
            return false;
         }
 
         else if (first < last)
         {
            return isPalindrome(first + 1, word, last - 1);
         }
 
         else
            return true;        
      }

Though I'm not sure about these 2 so make sure to give them a nice test if you decide to use them.

Edited 6 Years Ago by Nyight: n/a

Nyight, thank you very much, both of those solutions worked! I was not aware that you could use the method as the return value but that is very useful to know. I will mark this thread as solved.

@Nyight : I'm no master either, but please test the code you post!
I tested this:

public static bool isPalindrome(int first, string word, int last) 
        {
            if (word[first] != word[last])
            {
                return false;
            }
            else if (first < last)
            {
                return isPalindrome(first + 1, word, last - 1); ;
            }
            else return true;
        }
      
        static void Main(string[] args)
        {
            string word = "racecar";
            bool palindromeTest = isPalindrome(0, word, word.Length - 1); 
            if (palindromeTest == true) 
            { 
                Console.WriteLine("Yes the word is a palindrome."); 
            } 
            else Console.WriteLine("No, that word is not a palindrome");
            Console.ReadKey();
        }
This question has already been answered. Start a new discussion instead.