title: Recursive Function to generate / print a Fibonacci series
body: <p>I am trying to create a recursive function call method that would print the Fibonacci until a specific location:</p>
  
  <pre><code>1 function f = fibonacci(n)
  2 fprintf('The value is %d\n', n)
  3 if (n==1)
  4     f(1) = 1;
  5     return;
  6 elseif (n == 2)
  7     f(2) = 2;
  8 else
  9     f(n) = fibonacci(n-1) + fibonacci(n-2);   
  10 end
  11 end
  </code></pre>
  
  <p>As per my understanding the fibonacci function would be called recursively until value of argument n passed to it is 1. Then the function stack would rollback accordingly. So when I call this function from command:</p>
  
  <pre><code>&gt;&gt; fibonacci(4)
  </code></pre>
  
  <p>The value of n is 4, so line 9 would execute like:</p>
  
  <pre><code>9 f(4) = fibonacci(3) + fibonacci(2);
  </code></pre>
  
  <p>Now I believe that that first fibonacci(3) would be called - hence again for fibonacci(3)</p>
  
  <pre><code>9 if(3) = fibonacci(2) + fibonacci(1);
  </code></pre>
  
  <p>The ifs in line number 3 and 6 would take care.</p>
  
  <p>But now how  fibonacci(2) + fibonacci(1) statement would change to: </p>
  
  <pre><code> if(3) = 2 + 1;
  </code></pre>
  
  <p>I am receiving the below error and unable to debug further to resolve it:</p>
  
  <pre><code>&gt;&gt; fibonacci(4)
  The value is 4
  The value is 3
  The value is 2
  The value is 1
  In an assignment  A(I) = B, the number of elements in B and I must be the same.
  
  Error in fibonacci (line 9)
      f(n) = fibonacci(n-1) + fibonacci(n-2);
  
  Error in fibonacci (line 9)
      f(n) = fibonacci(n-1) + fibonacci(n-2);
  </code></pre>
  
  <p>Please provide some insight for the solution and with which parameter would fibonacci function be recursively called at line number 9 first and consequently.</p>
  
  <p>Ex For n = 4</p>
  
  <pre><code>f(n) = fibonacci(3) + fibonacci(2);
  </code></pre>
  
  <p>So will MATLAB call fibonacci(3) or fibonacci(2) first? </p>
  
  <p>Shouldn't the code be some thing like below:</p>
  
  <pre><code>1 function f = fibonacci(n)
  2 fprintf('The valus is %d\n', n)
  3 if (n==1)
  4     f(1) = 1;
  5     return f(1);
  6 elseif (n == 2)
  7     f(2) = 2;
  8    return f(2);
  9 else
  10   f(n) = fibonacci(n-1) + fibonacci(n-2);   
  11 end
  12 end
  </code></pre>
  
  <blockquote>
    <blockquote>
      <p>fibonacci(4)
      Error: File: fibonacci.m Line: 5 Column: 12
      Unexpected MATLAB expression.</p>
    </blockquote>
  </blockquote>
  
  <p>Why return expression in a function is resulting in an error?</p>