-1

I just wrote a function in C++ of checking whether a string is a palindrome.

bool isPal(string str, int s, int e)     //s= starting index and e= (size of string -1)
{
    if(s==e || s>e)
        return true;
    if(str[s]!=str[e])
        return false;
    return isPal(str, ++s, --e);         //using return
}

Calling Statement: cout<<isPal("aua",0,2);

Output: 1

On removing the return statement at the end, the function is producing a rather unexpected and arbitrary (which I'm not sure of) output.

Calling Statement: cout<<isPal("aua", 0 , 2);

Output: 144

Without the return statement at the end, the code looks like this:

bool isPal(string str, int s, int e)     //s= starting index and e= (size of string -1)
{
    if(s==e || s>e)
        return true;
    if(str[s]!=str[e])
        return false;
    isPal(str, ++s, --e);         //without return
}

What is the reason behind this output? And what difference does it make if I'm not using the return statement at the end of the recursion?

Ps: Coding environment Codeblocks.

ayush7ad6
  • 31
  • 4
  • 1
    Which toolchain are you using? Mingw? And what do you expect the function to return when an explicit 'return' statement is missing? I know that some programming languages simply use the output value of the last statement, however I do not think C (or C++ for that matter) is among them. – Refugnic Eternium Apr 07 '21 at 12:47
  • 3
    If you don't return for a non void return type, you have UB. – Jarod42 Apr 07 '21 at 12:47
  • 4
    Without the `return` on the one code path, the code has **undefined behavior**, which means it could appear to work, it could crash, it could cause the end of the world. That's the fun aspect of undefined behavior. – Eljay Apr 07 '21 at 12:47
  • Maybe refer to https://stackoverflow.com/questions/4644860/function-returns-value-without-return-statement – Refugnic Eternium Apr 07 '21 at 12:48
  • 1
    Whatever development environment you're in, it will be worth your time to find out how to enable all compiler warnings. The compiler *should* be telling you when your function is missing a return value. – Tim Randall Apr 07 '21 at 13:01
  • @RefugnicEternium Yes, I'm using Mingw. Actually, it was, you know, kind of testing with different options and learning with it. So, when I removed the return it produced an unexpected output, which made me question why that happened. Otherwise, the code with the return statement was running fine as I mentioned in the question. – ayush7ad6 Apr 07 '21 at 13:59
  • @Jarod42 If it's UB then why am I getting the same 144 value when trying with other strings? – ayush7ad6 Apr 07 '21 at 14:00
  • "Seems to works" is a possible output of UB. – Jarod42 Apr 07 '21 at 14:15

1 Answers1

0

According to the question at Function returns value without return statement, the return value is generally stored in the EAX register.

Executing return ... writes to this register, failing to do so will yield the same result as leaving a local variable uninitialized, meaning: 'The variable will have whatever value was in the memory location it got assigned'.

My personal favorite experience in this regard: A bool variable that ended up with a value of 42 in the debugger.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
  • So that means in this case it's 144? – ayush7ad6 Apr 07 '21 at 14:01
  • 1
    @ayush7ad6 It could be anything, really. Each time you run the program, it goes through the same motions, so, assuming that it always writes the return value to the exact same location, it will also always return the exact same value, namely: Whatever was written to that location last, namely: The return value of the last function with a return value. Though that does not seem to be the case here, otherwise it would actually return the correct value of true/false (because somewhere down the recursion, there will be a return value). – Refugnic Eternium Apr 08 '21 at 06:22