title: Why I get 0 and 1 in the following golang code example with defer
body: <p>Call to defer produces different results for variables declared in two different ways</p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  func c(i int) int {
      defer func() { i++ }()
      return i
  }
  
  func c1() (i int) {
      defer func() { i++ }()
      return i
  }
  
  func c2() (i int) {
      defer func() { i++ }()
      return 2
  }
  
  func main() {
      fmt.Println(c(0)) // Prints 0
      fmt.Println(c1()) // Prints 1
      fmt.Println(c2()) // Prints 3 Thank you icza
  }
  </code></pre>
  
  <p><a href="https://play.golang.org/p/gfnnCZ--DkH" rel="nofollow noreferrer">https://play.golang.org/p/gfnnCZ--DkH</a></p>