Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func f(x int) int {
return 1 + x*2
}
func main() {
fmt.Println(f(f(2)))
}11
- Expecting
14:*binds tighter than+, so1 + x*2is1 + (x*2), not(1 + x)*2;f(2)is5andf(5)is11. - Expecting
5or5 11: the inner call's result5is passed tofagain, andfmt.Printlnprints only the value of its argument,f(5).
- What would
fmt.Println(f(2), f(f(2)))print? - How would you change
fso that it computes(1 + x) * 2?