Hi there,

I have a question related to C++ functions. If a boolean return type function contains too many returns in it, does it cause any problem ??

In my program one of the bool function contains too many return statements, in if conditions. And interesting thing is that it is not giving an error and the program is not crashing but boolean function returns 128 value instead of 1 or 0.

Any idea, why it is giving this unusual result ?? The method is as follows. Thanks !

        bool pathClear(int currentRow, int inputRow, int currentCol, int inputCol)
        {
            pathIsClear = false;
            string currentCellValue = board[currentRow][currentCol];

            if(checkInput(currentRow, inputRow, currentCol, inputCol) == true)
            {
                //conditions for player-1
                if(currentCellValue[0] == 'p' && currentCellValue[1] == '1')
                {

                    if( (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == true && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[0] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[0] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[0] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '2' && firstTimeMoveArr_1[1] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_1[1] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[1] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[1] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[1] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '3' && firstTimeMoveArr_1[2] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_1[2] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[2] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[2] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[2] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '4' && firstTimeMoveArr_1[3] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_1[3] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[3] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[3] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[3] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '5' && firstTimeMoveArr_1[4] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_1[4] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[4] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[4] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[4] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '6' && firstTimeMoveArr_1[5] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_1[5] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[5] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[5] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[5] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '7' && firstTimeMoveArr_1[6] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_1[6] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[6] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[6] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[6] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '8' && firstTimeMoveArr_1[7] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_1[7] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_1[7] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_1[7] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_1[7] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else
                        return false;
                }
                //conditions for player-2
                else if(currentCellValue[0] == 'p' && currentCellValue[1] == '2')
                {
                    if( (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[0] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[0] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[0] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '2' && firstTimeMoveArr_2[1] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_2[1] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[1] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[1] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[1] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '3' && firstTimeMoveArr_2[2] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_2[2] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[2] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[2] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[2] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '4' && firstTimeMoveArr_2[3] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_2[3] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[3] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[3] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[3] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '5' && firstTimeMoveArr_2[4] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_2[4] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[4] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[4] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[4] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '6' && firstTimeMoveArr_2[5] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_2[5] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[5] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[5] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[5] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '7' && firstTimeMoveArr_2[6] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_2[6] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[6] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[6] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[6] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else if( (currentCellValue[5] == '8' && firstTimeMoveArr_2[7] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_2[7] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
                    {
                        if(firstTimeMoveArr_2[7] == false)
                        {
                            pathIsClear = true;
                            firstTimeMoveArr_2[7] == true;
                            return true;
                        }
                        else if(firstTimeMoveArr_2[7] == true)
                        {
                            pathIsClear = true;
                            return true;
                        }
                    }
                    else
                        return false;
                }
            }
            else
            {
                return false;
            }
        }

Each return point returns only one value, true or false. Assuming your logic is valid, you are ok. FWIW, I am NOT going to review all 250 lines of your code to determine if your predicate logic is valid! :-)

So, which branch is returning "128" instead of 0 or 1?

If a boolean return type function contains too many returns in it, does it cause any problem

No -- The acual value of true and false is not defined, nor is it guarenteed to be either 0 or 1. It's actual value depends on the compiler -- 128 may be the correct value for your compiler. In Microsoft Visual C++ 6 and newer versions sizeof(bool) is one byte. The max value of a signed char is 127, so the value of unsigned char of 128 could be -1 in signed char. Apparently your compiler treates true as -1 and false as 0.

The acual value of true and false is not defined, nor is it guarenteed to be either 0 or 1

Not sure what you mean by "actual value", but converting true to an integer will give 1 and false will give 0. That's defined in section 3.9.1, paragraph 6 (in the C++11 standard, but I'm pretty sure it's the same in C++03 - I just don't have the 03 standard handy right now).

The only way you'd get a bool that prints as 128 is if there's UB being invoked somewhere - presumably due to a missing return statement somewhere in the function.

I know microsoft uses -1 as true if you are using access so it makes sense that they are doing that with there compiler. AFIK any value other than 0 is considered true and I havent seen a case where that doesnt work.

yeah all of you are correct, in my code return statement is missing in some of if conditions. That means all of if conditions having return true statement then if must have return false statement in else and vice versa.

Thanks for great support !

What is UB sepp2k ??

UB = Undefined Behavior, which means the standards do not say what happens, and the compilers are free to do whatever they wish. Such behavior may be different from one compiler to the next, or sometimes the program may just crash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.