title: How can you pass an int variable into a function with float parameters in C++ without getting compile errors?
body: <p>My professor gave us two examples of code in which both pass the int
  values, x &amp; y, into a function with float parameters as examples of
  how to pass a different type of variable into a function by reference. I've ran them both, and neither work, unless of course I change <code>int x, y;</code> to <code>float x, y;</code>. The programs are written in C. </p>
  
  <p>In his notes, following the 2nd program, he makes his point regarding the comparison of the 2: </p>
  
  <p><strong>"It (both programs) prints out:  normalized vector = ( .12, .16 )
  This code is nearly identical to the code of the previous example.  The difference is in the way the parameters are declared in the normalizeVector function.  By using the "&amp;" operator, the normalizeVector function has access to the variables being passed to it, not just access to the values of those variables, access to the original location.  This is functionally the same as having the address of those variables, but the syntax is nicer for those not used to using C pointers.  Since the "&amp;" is used, all of the work de-referencing the variables using the "*", as in the previous example, can be avoided."</strong></p>
  
  <p>While I am not questioning the rule or technique, I can't run them and am dying to know what stupid little mistake I am making or what crazy (and probably very simple) detail I am missing. I have done much more than just copy and paste them. Not to mention, when I used <code>int</code> <code>float</code> to make them work, I had to remove the <code>sqrt</code> before <code>(xWork*xWork + yWork*yWork)</code> in order to get the correct answers. (And by the way, he is a great professor.) Should these programs work? And how do I pass <code>int</code> into <code>float</code> parameters by reference, if they don't? </p>
  
  <h3>Program 1</h3>
  
  <pre><code>main()
  {
      int x, y;   
      x = 3;
      y = 4; 
  
      normalizeVector(&amp;x, &amp;y);    
      printf(“normalized vector = ( %f, %f ) \n”, x, y);
  }  
  
  void normalizeVector(float *x, float *y)
  {
      float magnitude;
      float xWork, yWork;
      xWork = *x;
      yWork = *y; 
  
      magnitude = sqrt(xWork*xWork + yWork*yWork);
      *x = xWork/magnitude;
      *y = yWork/magnitude;
  }
  </code></pre>
  
  <h3>Program 2</h3>
  
  <pre><code>main()
  {
      int x, y;    
      x = 3;
      y = 4;
  
      normalizeVector(x, y);
      printf(“normalized vector = ( %f, %f ) \n”, x, y);
  }  
  
  void normalizeVector(float &amp;x, float &amp;y)
  {
      float magnitude;
      float xWork, yWork;
      xWork = x;
      yWork = y;
  
      magnitude = sqrt(xWork*xWork + yWork*yWork);
      x = xWork/magnitude;
      y = yWork/magnitude;
  }
  </code></pre>