title: Objective C: Returning Objects from Methods
body: <p>I'm learning how to program, and starting off with Objective C. I'm trying to understand exactly what happens when an object is allocated from within a method. </p>
  
  <pre><code>-(Fraction *) add: (Fraction *) f
  {   //'result' will store the result of the addition
  
  Fraction *result = [[Fraction alloc]init];
  
  result.numerator = (numerator*f.denominator + denominator*f.numerator);
  result.denominator = denominator*f.denominator;
  
  [result reduce];
  return result;
  }
  </code></pre>
  
  <p>I understand that I can create an object to store 'result' in when it is returned, </p>
  
  <pre><code>tempStorageObject = [aFraction add: bFraction];
  </code></pre>
  
  <p>and that i am then responsible for releasing it, but what happens when I don't store it, as in:</p>
  
  <pre><code>[aFraction add: bFraction];
  NSLog(@"%i/%i", result.numerator, result.denominator); //result object not declared in main
  </code></pre>
  
  <p>I am told that I have an undeclared identifier. I get that, but what exactly happens to 'result' after using my 'add' method. Where does it go? Shouldn't I be able to access its variables since it was created and returned in the method? Obviously not, but I'm not clear on why. I've tried re-reading my book and searching this forum but I can't find a clear answer. Thanks. (First post)</p>