1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

not all code paths return a value

Discussion in 'C#' started by jc1forall, Sep 3, 2010.

  1. jc1forall

    jc1forall New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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:
    Code:
     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.
     
  2. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    7
    Trophy Points:
    0
    Your main function is not returning any value and so you are getting that error.
    Code:
             if (palindromeTest == true)
             {
                Console.WriteLine("Yes the word is a palindrome.");
             }
             else
                Console.WriteLine("No, that word is not a palindrome");
    Just make sure you have a return statement after the if else or inside each of if else.
     

Share This Page