lundi 24 février 2020

Boolean function with reference parameter

I have been searching around and I cant quite get the answer I looking for or see the example I want. I'm working with a binary tree. I have a bool search function that checks the whole tree. I currently have this

  bool search(int key)
  {
    NODE* cur = Root;

    while (cur != nullptr)
    {
      if (key == cur->Key)  // already in tree
        return true;

      if (key < cur->Key)  // search left:
      {
        cur = cur->Left;
      }
      else
      {
        cur = cur->Right;
      }
    }//while  

    // if get here, not found
    return false;
  }

But now i want to modify this to as if the key is found the corresponding value is also returned as a reference parameter. So adding, and having the function declaration be

bool search(int key, int& value)

If this is the case can I just declare int value on top of when I return true and have value equal whatever i want it to pass in my case what the value is in that key?

Aucun commentaire:

Enregistrer un commentaire