title: Fixing Tic-Tac-Toe Code Error.. Doesn't Run Due to DeclareWinner function bein written later. How can I correct it?
body: <p>The entire code for my Tic Tac Toe game has been pasted below. It doesn't run due to an error in the Declare Winner function being added after its use. How do I correct this?
  Do I simply create it earlier?</p>
  <p>Use of local variable 'DeclareWinner' before its declaration. This is what it says. What am I supposed to do?</p>
  <pre><code>  import UIKit
  
    class GameViewController: UIViewController {
  
    @IBOutlet weak var MainTitle: UILabel!
  
    @IBOutlet weak var Player1Label: UILabel!
    @IBOutlet weak var Player2Label: UILabel!
  
    @IBOutlet weak var PlayerTurnLabel: UILabel!
  
    @IBOutlet weak var GameONLabel: UILabel!
  
  var matrix: [[Int]] = [[0,0,0],[0,0,0],[0,0,0]]  // 0 - Nothing, 1 - X, 2 - O
  
  var WhosTurn = 0 // 1- Player1, 2- Player2
  
  var Player1 = 0 // 0 - Nothing, 1 - X, 2 - O
  var Player2 = 0 // 0 - Nothing, 1 - X, 2 - O
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      // Do any additional setup after loading the view.
      
      StartGame()
      
  }
  
  func StartGame(){
      
      matrix = [[0,0,0],[0,0,0],[0,0,0]]   // 0 - Nothing, 1 - X, 2 - O
      displayMat(matrix: matrix)
      
      WhosTurn = Int.random(in: 1...2)
      
      if (WhosTurn == 1){ // Player1's Turn and He's X
          
          Player1 = 1
          Player2 = 2
          
          Player1Label.text = &quot;Player 1: X&quot;
          Player2Label.text = &quot;Player 2: O&quot;
          
          PlayerTurnLabel.text = &quot;Turn: Player 1&quot;
          
          
      }else {// Player2's Turn and He'll be O
          
          Player1 = 2
          Player2 = 1
          Player1Label.text = &quot;Player 1: O&quot;
          Player2Label.text = &quot;Player 2: X&quot;
          
          PlayerTurnLabel.text = &quot;Turn: Player 2&quot;
      }
      
  }
  
  func displayMat(matrix: [[Int]]) {
      
      
      var TempTag = 1
      for i in 0...2  {
          for j in 0...2  {
              if (matrix[i][j] == 0 )  {
                  let TempButton = self.view.viewWithTag(TempTag) as? UIButton
                  TempButton?.setBackgroundImage(nil, for: .normal)
                  
              }  else if (matrix[i][j] == 1) { //Display X
                  let TempButton = self.view.viewWithTag(TempTag) as? UIButton
                  TempButton?.setBackgroundImage(UIImage(named: &quot;X&quot;), for: .normal)
  
              }else if (matrix[i][j] == 2)  { //Display O
                  let TempButton = self.view.viewWithTag(TempTag) as? UIButton
                  TempButton?.setBackgroundImage(UIImage(named: &quot;O&quot;), for: .normal)
  
              }else {
                  
              }
              
              TempTag += 1
          }
      }
      
  }
  
  
  @IBAction func ButtonClickedXorO(_ sender: Any) {
      
      var TempObject = 0
      
      if (WhosTurn == 1){ // Player1's Turn and He's X
          TempObject = Player1
          Player1 = 1
          Player2 = 2
          
          Player1Label.text = &quot;Player 1: X&quot;
          Player2Label.text = &quot;Player 2: O&quot;
          
          PlayerTurnLabel.text = &quot;Turn: Player 1&quot;
          
          
      }else {// Player2's Turn and He'll be O
          TempObject = Player2
          PlayerTurnLabel.text = &quot;Turn: Player 2&quot;
      }
      
      guard let button = sender as? UIButton else {
          return
      }
      
      switch button.tag {
      case 1:
          if (matrix[0][0] == 0) {
              matrix[0][0] = TempObject
              JustPlayed()
          }
      case 2:
      if (matrix[0][1] == 0) {
          matrix[0][1] = TempObject
          JustPlayed()
      }
      case 3:
      if (matrix[0][2] == 0) {
          matrix[0][2] = TempObject
          JustPlayed()
      }
      case 4:
      if (matrix[1][0] == 0) {
          matrix[1][0] = TempObject
          JustPlayed()
      }
      case 5:
      if (matrix[1][1] == 0) {
          matrix[1][1] = TempObject
          JustPlayed()
      }
      case 6:
      if (matrix[1][2] == 0) {
          matrix[1][2] = TempObject
          JustPlayed()
      }
      case 7:
      if (matrix[2][0] == 0) {
          matrix[2][0] = TempObject
          JustPlayed()
      }
      case 8:
      if (matrix[2][1] == 0) {
          matrix[2][1] = TempObject
          JustPlayed()
      }
      case 9:
      if (matrix[2][2] == 0) {
          matrix[2][2] = TempObject
          JustPlayed()
      }
          
      default:
          print(&quot;Error&quot;)
      }
  }
  
  func JustPlayed(){
      
      displayMat(matrix: matrix)
      Winner()
      
      if (WhosTurn == 1) {
          
          WhosTurn = 2
          PlayerTurnLabel.text = &quot;Turn: Player 2&quot;
          
      }else {
          WhosTurn = 1
          PlayerTurnLabel.text = &quot;Turn: Player 1&quot;
      }
  
  }
  
  func Winner(){
      
      var Counter = true
      
      if ((matrix[0][0] == matrix[1][1] &amp;&amp; matrix[1][1] == matrix[2][2] &amp;&amp; matrix[1][1] != 0))
        DeclareWinner(WhoWon: matrix[1][1])
          
      }else if ((matrix[2][0] == matrix[1][1] &amp;&amp; matrix[1][1] == matrix[0][2] &amp;&amp; matrix[1][1] != 0)) {
          DeclareWinner(WhoWon: matrix[1][1])
          
      }else {
          
          for i in 0...2{
              if (matrix[i][0] == matrix[i][1] &amp;&amp; matrix[i][1] == matrix[i][2] &amp;&amp; matrix[i][0] != 0){
                  DeclareWinner(WhoWon: matrix[i][0])
                  Counter = false
              }
          }
          
          if Counter{
              for i in 0...2{
                  if (matrix[0][i] == matrix[1][i] &amp;&amp; matrix[1][i] == matrix[2][i] &amp;&amp; matrix[0][i] != 0){
                      DeclareWinner(WhoWon: matrix[0][i])
                  }
              }
          
      }
      
  }
  
  func DeclareWinner(WhoWon: Int){
      if (WhoWon == Player1) {
          // Player1 Won
          GameONLabel.text = &quot;Player 1 Won!&quot;
          
      }else if (WhoWon == Player2){
          //Player2 Won
          GameONLabel.text = &quot;Player 2 Won!&quot;
          
      }
      
  }
  /*
  // MARK: - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      // Get the new view controller using segue.destination.
      // Pass the selected object to the new view controller.
  }
  */
  
  @IBAction func GoBack(_ sender: Any) {
      dismiss(animated: true, completion:nil)
  }
  </code></pre>
  <p>}</p>