title: Arduino - Variables declared in setup() not in scope of function
body: <p>I planned to write a function that modifies a two-dimensional array so that each coordinate is set to 0. In <code>setup()</code> I declared the <code>displayWidth</code> and <code>displayHeight</code> but can't access them in the <code>generateBoard()</code> function because they aren't in the same scope.</p>
  
  <h2>The code:</h2>
  
  <pre><code>void generateBoard(int board[][]) {
    // Modifies the array board by setting zeros
  
    for (int y=0; y &lt; displayHeight; y++) {
      for (int x=0; x &lt; displayWidth; x++) {
        board[x][y] = 0;
      }
    }
  }
  
  void setup() {
    int displayWidth  = 14;
    int displayHeight = 10;
  
    int board[displayWidth][displayHeight];
    generateBoard(board); 
  }
  
  void loop() {}
  </code></pre>
  
  <p><strong>Exception with local scope inside setup()</strong></p>
  
  <pre><code>error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
  error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
  In function 'void generateBoard(...)':
  error: 'displayHheight' was not declared in this scope
  error: 'displayWidth' was not declared in this scope
  error: 'board' was not declared in this scope
  </code></pre>
  
  <hr>
  
  <h2>Fixed and working version:</h2>
  
  <pre><code>const int displayWidth  = 14;
  const int displayHeight = 10;
  int board[displayWidth][displayHeight];
  
  void generateBoard() {
    // Modifies the array board by setting zeros
  
    for (int y=0; y &lt; displayHeight; y++) {
      for (int x=0; x &lt; displayWidth; x++) {
        board[x][y] = 0;
      }
    }
  }
  
  void setup() {
    generateBoard(); 
  }
  
  void loop(){}
  </code></pre>