Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func inc(n int) int {
n++
return n
}
func main() {
x := 1
fmt.Println(inc(x), x)
}2 1
- Expecting
2 2:nis a copy ofx, son++changes only the copy;increturns2andxstays1. - Expecting
1 1or a compile error: a parameter is an ordinary variable of the function, son++is allowed and changesn, whichincthen returns.
- How would you change
mainso thatxitself becomes2? - What would the program print if
incnamed its parameterxinstead ofn?