title: How to initialize a struct with a function in C++?
body: <p>I want to make a program that does fraction calculations, and i've made a fraction struct with 2 properties: numerator and denominator. I want to be able to initialize those properties using a function i've overloaded twice called <code>init(fraction x)</code>. The overloads are as follows:</p>
  
  <pre><code>init(fraction x) //Initializes a fraction struct with numerator and denominator equal to 0
  init(fraction x, int a) //initializes fraction struct to a/1
  init(fraction x, int a, int b) //initializes fraction struct to a/b
  </code></pre>
  
  <p>Then I try to test it and output a fraction like so</p>
  
  <pre><code>int main() {
  
  fraction a;
  init(a,1);
  
  cout &lt;&lt; "The fraction is: ";
  print(a); 
  cout &lt;&lt; endl;
  
  return 0;
  }
  </code></pre>
  
  <p>but I just get 0/0 every time no matter what. I think it's a pass by value/reference problem but i'm not sure how to go about fixing this. Here's all my code:</p>
  
  <p>Header.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct fraction {
      int numerator = NULL, denominator = NULL;
  };
  
  void init(fraction x);                          // set fraction to 0
  void init(fraction x, int n);                // set fraction to n/1
  void init(fraction x, int a, int b);       // set fraction to a/b
  fraction add(fraction a, fraction b);                        // adds a and b and returns the sum (a and b unchanged)
  fraction sub(fraction a, fraction b);                        // subtracts a and b and returns difference
  fraction mul(fraction a, fraction b);                        // multiplies a and b and returns the product
  fraction div(fraction a, fraction b);             // divides a and b and returns the quotient
  void print(fraction x);                                    // prints the fraction (no improper fractions)
  void read(fraction x);                                    // reads fraction 3 / 5 into x
  void reduce(fraction x);                               // simplifies x to lowest common terms
  int gcd(int a, int b);                                       // finds greatest common divisor of a and b
  </code></pre>
  
  <p>Source.cpp</p>
  
  <pre><code>#include "Header.h"
  
  void init(fraction x) {
      x.numerator = 0;
      x.denominator = 0;
  }
  
  void init(fraction x, int n) {
      x.numerator = n;
      x.denominator = 1;
  }
  
  void init(fraction x, int a, int b) {
      x.numerator = a;
      x.denominator = b;
  }
  
  
  fraction add(fraction a, fraction b) {
      return a;
  }
  
  
  fraction sub(fraction a, fraction b) {
      return a;
  }
  
  fraction mul(fraction a, fraction b) {
      return a;
  }
  
  fraction div(fraction a, fraction b) {
      return a;
  }
  
  void print(fraction x) {
      cout &lt;&lt; x.numerator &lt;&lt; '/' &lt;&lt; x.denominator;
  }
  
  void read(fraction x) {
  
  }
  
  void reduce(fraction x) {
  
  }
  
  int gcd(int a, int b) {
      return a;
  }
  </code></pre>
  
  <p>Main.cpp</p>
  
  <pre><code>#include "Header.h"
  
  int main() {
  
  fraction a;
  a.numerator = a.denominator = 1;
  init(a,1, 2);
  
  cout &lt;&lt; "The fraction is: ";
  print(a); 
  cout &lt;&lt; endl;
  
  return 0;
  }
  </code></pre>