title: Best way to pass local variable to function in C++
body: <p>i just wanted to know what is the best way to pass a local variable to a function.</p>
  
  <pre><code>void check2 (int* var2){
      *var2=7;
  }
  
  void check1 (int&amp; var){
      var=6;
  }
  
  int main()
  {
      int var;
      int* var2=new int;
  
      check1(var);
      check2(var2);
  
      delete var2;
  
      return 0;
  }
  </code></pre>
  
  <p>In <code>check1</code>, I pass the variable using a reference. As I am passing a local variable to the function <code>check1</code>, wouldn't it get out of scope once main terminates and there would be no variable anymore?</p>
  
  <p>I found a couple of examples where the <code>operator new</code> is used to allocate memory and return a pointer which is then passed to the function. Is this a better way to do the same task as the variable doesn't get erased?</p>