title: C - Returning a Structure Member from Function
body: <p>I am writing this code that calculates Miles per Gallon where the number of miles and gallons are read from the keyboard. The program will perform a simple calculation to get the miles per gallon. I wrote a function in order to do this this calculation but it is not returning the result. Inside the function everything is working as it should. I even wrote a <code>printf</code> to ensure the calculation was done correctly. But then when I try to return it to the main function, the member of the structure appears to have a value of <code>0</code> instead of the result of the calculation. Here is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct gas {
   float distance;
   float gals;
   float mpg;
  };
  
  float CalculateMpg(struct gas idk){
      idk.mpg = idk.distance / idk.gals;
      printf(&quot;%f \n&quot;, idk.mpg);
      return idk.mpg;
  }
  
  int main()
  {
      struct gas idk;
  
      printf(&quot;Enter distance (in miles): &quot;);
      scanf(&quot;%f&quot;, &amp;idk.distance);
      printf(&quot;Enter gallons fueled: &quot;);
      scanf(&quot;%f&quot;, &amp;idk.gals);
  
      CalculateMpg(idk);
      printf(&quot;%f \n&quot;, idk.mpg);
      printf(&quot;Miles driven gallons used with %.2f miles and %.2f gallons: %.2f mpg \n&quot;, idk.distance, idk.gals, idk.mpg);
  
      return 0;
  }
  </code></pre>