title: C++, recursive correct answer but not being returned correctly
body: <p>Ok, so heres my simple program for calculating Greatest common divisor. It is returning a garbage value everytime the same as 46332964. Ive thought over my head for an hour but cant seem to understand the problem. After research, i also included the prototype but still no luck. It works until it returns. Please copy the code and run it and please help me.  </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int calculate_gcd(int aa, int bb, int rem, int g);
  
  int main()
  {
      int a, b;
      int rem = -1;
      int gcd=0;
  
      cout &lt;&lt; "Number 1: "; //taking inputs
      cin &gt;&gt; a;
      cout &lt;&lt; "Number 2: ";
      cin &gt;&gt; b;
  
      if (a &lt; b) //swapping if number greater than the number according to the code
      {
         a = a + b;
         b = a - b;
         a = a - b;
      }
  
      gcd = calculate_gcd(a, b, rem, gcd);
  
      if (a &lt;= 0 || b &lt;= 0)
      {
         rem = 0;
         cout &lt;&lt;"GCD doesnot exists\n";
      } //just in case of zeros
      else
          cout &lt;&lt; "\nthe GCD of "&lt;&lt;a &lt;&lt;" and "&lt;&lt;b &lt;&lt;" is "&lt;&lt;gcd &lt;&lt;"\n\n"; //the main answer
  
      system("pause");
      return 0;
  }
  
  int calculate_gcd(int aa, int bb, int rem, int g)
  {
      if (rem != 0)
      {
         if (aa%bb == 0)
          {
             rem = 0;
             g = bb;
             printf("**GCD is %d\n", g);
          }
          else {
               rem = aa % bb;
               aa = bb;
               bb = rem;
          }
          calculate_gcd(aa, bb, rem, g);
      }
      else {
           printf("**here also GCD is correct as %d \n", g);
           return g; //returning 
           }
  }
  </code></pre>