Home  >  Article  >  Backend Development  >  Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 04:04:27308browse

Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Why Does Recursive Code Return None?

In the provided code snippet, a recursive function, isIn, is defined to determine whether a given character exists within a string. However, it consistently returns None instead of the expected True value when the character is found within the string.

The code performs a binary search on the string, repeatedly dividing it in half. When the character is found at the midpoint, it prints a message indicating its location, but it fails to return True.

To rectify this issue, a return statement should be added to the last line of the function, as seen below:

<code class="python">return isIn(char, aStr)</code>

Without this return statement, the function simply returns None when it terminates without encountering a return statement. By adding this return, the function can properly return True when the character is located within the string.

The above is the detailed content of Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn