title: How does scoping in Matlab work?
body: <p>I just discovered (to my surprise) that calling the following function</p>
  
  <pre><code>function foo()
  if false
     fprintf = 1;
  else
    % do nothing
  end
  fprintf('test')
  </code></pre>
  
  <p>gives and error <code>Undefined function or variable "fprintf"</code>. My conclusion is that the scope of variables is determined before runtime (in my limited understanding how interpretation of computer languages and specifically Matlab works). Can anyone give me some background information on this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Another interesting thing I forgot to mention above is that</p>
  
  <pre><code>function foo()
  if false
     fprintf = 1;
  else
    % do nothing
  end
  clear('fprintf')
  fprintf('test')
  </code></pre>
  
  <p>produces <code>Reference to a cleared variable fprintf</code>.</p>