title: Passing by Reference to Function Correctness
body: <p>I am working on a graph traversal task and I have three variables which I would like to pass as arguments to be modified within the function so that after the function has finished executing, the values to which those references referred to are now changed (by the function). These 3 variables are: max, minnd, and cnt. You can see in the dfs() function how I passed them by reference. <strong>It is my first time ever passing by reference.</strong> <em>My question is: have I passed the references correctly, and is my idea to pass by reference to change an outside variable the right way to do that?</em> (MY CODE IS BELOW)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  int numv, nume, totd = 0, toti = 0;
  vector&lt;pair&lt;int, vector&lt;int&gt; &gt; &gt; list;
  bool seen[100002];
  vector&lt;int&gt; lit;
  
  void dfs(int v, int &amp;max, int &amp;minnd, int &amp;injcnt)
  {
      for (int i = 0; i &lt; list[v].second.size(); i++)
      {
          if (!seen[list[v].second[i]])
          {
              seen[list[v].second[i]] = true;
              if (list[v].first &gt;= max)
              {
                  max = list[v].first;
                  if (v &lt; minnd) minnd = v;
              }
              injcnt++;
              dfs(list[v].second[i], max, minnd, injcnt);
          }
      }
  }
  
  int main()
  {
      list.resize(100002);
      cin &gt;&gt; numv &gt;&gt; nume;
      for (int i = 1; i &lt;= numv; i++)
      {
          cin &gt;&gt; list[i].first;
          seen[i] = false;
      }
      for (int i = 1; i &lt;= nume; i++)
      {
          int v1, v2;
          cin &gt;&gt; v1 &gt;&gt; v2;
          list[v1].second.push_back(v2);
          list[v2].second.push_back(v1);
      }
      for (int i = 1; i &lt;= numv; i++)
      {
          if (!seen[i])
          {
              seen[i] = true;
              int max = 0, minnd = -1, cnt = 0;
              dfs(i, max, minnd, cnt);
              totd += max;
              toti += cnt;
              lit.push_back(minnd);
          }
      }
      sort(lit.begin(), lit.end());
      cout &lt;&lt; totd &lt;&lt; " " &lt;&lt; toti &lt;&lt; "\n";
      vector&lt;int&gt;::iterator it;
      for (it = lit.begin(); it != lit.end(); it++)
          cout &lt;&lt; *it &lt;&lt; " ";
      cout &lt;&lt; "\n";
      return 0;
  }
  </code></pre>