Tutorial :recursive method - not all code paths return a value!



Question:

it says not all code paths return a value

private string Fisrt(string nonTerminal)      {          for (int j = 0; j < 6; j++)          {              if (Tokens[j, 0] == nonTerminal)              {                  if (char.IsLower((char)Tokens[j, 3][0]))                      return (Tokens[j, 3]);                  else                      Fisrt(Tokens[j, 3]);              }          }      }  


Solution:1

private string Fisrt(string nonTerminal)      {          for (int j = 0; j < 6; j++)          {              if (Tokens[j, 0] == nonTerminal)              {                  if (char.IsLower((char)Tokens[j, 3][0]))                      return (Tokens[j, 3]);                  else                      return Fisrt(Tokens[j, 3]);                   /* ^ add a return here */              }          }            return SOMETHING;       /* ^ You also need to add some return value here */      }  

You also need to decide what string value (or null) to return in the event your for loop exits normally.


Solution:2

For example, what if none of the Tokens[j, 0], with j values 0 to 5, is nonTerminal?

Or, if Tokens[j, 3][0] is never lowercase?


Solution:3

You should return the recursive step

`return First(Tokens[j, 3])`  

and handle the cases outside the outer for and if.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »