Possible to leave recursion prematurely?

There is a way to exit recursion using exceptions, but I wouldn't recommend it. Instead, modify your function to return a bool that indicates whether you've found 0 or not and modify your logic to return from the function without changing path if 0 has been found. Here's the illustration of the idea:

bool Graph::findPath( Room * curRoom )
{
    if( curRoom -> myNumber == 0 )
    {
        cout << "Outside.\n";
        //Escape the recursion!
        return true;
    }
    // ...
    if (findPath( curRoom -> North ))
        return true;
    // ...
    return false;
}

Tags:

C++

Recursion