0

I am just messing around with the function and somehow palindromes returns 74. I am using Visual studio 2022. Was it supposed to not return anything and catch compiler error since false is never returned in the case below?

bool palindromes(string str) {
    if (str.length() == 0 || str.length() == 1) return true;

    if (str[0] == str[str.length() - 1])
        palindromes(str.substr(1, str.length() - 2));
    else
        return false;
}


int main()
{
    cout << palindromes("lol");
} 
Starlight
  • 27
  • 1
  • 5
  • 6
    You forgot to actually return something from the function. – user253751 May 11 '22 at 10:04
  • Do the answers to this [question](https://stackoverflow.com/questions/15960015/c-printing-boolean-what-is-displayed) help at all? – quamrana May 11 '22 at 10:05
  • 4
    Undefined behavior is undefined behavior. I suggest compiling with warnings turned up (and reading them when they happen), so the compiler tells you when you forget to return a value on certain code paths. – ShadowRanger May 11 '22 at 10:10
  • 2
    From C++ Standard, 6.6.3: Flowing off the end of a function is equivalent to a return with no value; this results in undefined behaviour in a value-returning function. – Gonen I May 11 '22 at 10:12
  • Off-topic: This recursive variant of creating substrings again and again is pretty inefficient! Rather make this function a frontend for an actual worker iterating on iterators. And you actually need one single test as stop condition: `str.length() <= 1`... – Aconcagua May 11 '22 at 10:12
  • Have you considered an iterative variant? `if(!str.empty()) { auto b = str.begin(); e = std::prev(str.end()); while(b < e) { if(*b != *e) { return false; } } return true;` – Aconcagua May 11 '22 at 10:17

3 Answers3

5

palindromes(str.substr(1, str.length() - 2)); is a typical mistake with recursion. I suppose you want to do something with the value returned from that call, but you ignore it. Recursive functions work very much the same as non-recursive functions, there is no implicit return.

Very much related, the function does not return something when this condition str.length() == 0 || str.length() == 1 is false but this condition str[0] == str[str.length() - 1] is true.


This line

palindromes(str.substr(1, str.length() - 2));

Does nothing. The function gets a parameter passed by value and the returned value is ignored.

Not returning something from a function that is declared to return something is undefined behavior.


Fix:

return palindromes(str.substr(1, str.length() - 2));

There might be other issues though.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Seems that you ignored the warning:

warning C4715: 'palindromes': not all control paths return a value

This is a sin.

-1

Don't use recursion for what is a linear problem. Your multiple string copies add unnecessary overhead.

An alternative:

return std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin());

This is readable and therefore less prone to bugs - your function not returning a value on all control paths is a bug: str.length() - 2 is vulnerable to unsigned arithmetic.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483