The question
The quiz shows a function definition and asks what happens if you run it:
func sayMyName() {
fmt.Println("Heisenberg!")
}
The options are: A, it prints Heisenberg!; B, it produces errors; C, it prints nothing; D, it prints sayMyName. Your task is to store the letter of the right option in a string variable named answer.
How to work out the answer
Ask two questions, in this order.
- Is the code valid? The header has the keyword
func, a name and parentheses, and the body is wrapped in braces. The call tofmt.Printlnis inside the body. Nothing is wrong, so option B is out. - Is the function ever called? No line writes
sayMyName(). A definition only tells Go that the function exists. The body runs when the function is called, and here nobody calls it.
So nothing is printed, and the answer is C. Option A would be right if there were a call such as sayMyName(). Option D confuses the function's name with the text inside the string: the name is never printed unless you pass it to Println as a string.
What would change the answer
Compare the quiz code with a version that has a call:
func sayMyName() {
fmt.Println("Heisenberg!")
}
func mainFunction() {
sayMyName()
}
Now mainFunction contains sayMyName(): the name followed by parentheses. Running mainFunction prints Heisenberg! once. Remove that single line and the output is empty again. The definition is identical in both versions, so the call alone decides whether anything is printed.
Two other changes would alter the result. A second sayMyName() line would print the message twice. A missing brace or a misspelled Println would make the code invalid, and then B would be right. The quiz code has neither problem.
The solution in Go
The lesson's code sits inside a function, and the answer is declared inside it, before the line that prints it:
func magicNumber(n int) {
var answer string = "C"
/* Explanation:
Remember, the code inside a function will not be executed unless we call that function!
In our code, we only define the function sayMyName(), without calling it afterwards, so the fmt.Println() won't be executed & nothing will be printed to the console.
*/
fmt.Println(answer)
}
The Go details in the answer line:
var answer string = "C"declares a variable namedanswerwith the typestringand the value"C". Strings use double quotes. Single quotes make a rune, a different type, so'C'would not be a string.- The short form
answer := "C"declares the same thing and lets Go infer the type. The lesson asks for thevarform. - The variable must be used. Go rejects a local variable that is declared and never used. The
fmt.Println(answer)line uses it, which is why the line stays in place. - It must be declared first. Using
answerbefore the declaration is a compile error, so the line goes above thePrintln. - The letter is a string, not a bare name. Writing
Cwithout quotes would name a variable that doesn't exist.
Common mistakes
- Assuming that defining a function runs it. This is the lesson's main point. A definition and a call are two different things.
- Picking B. The code is valid Go. A function that is never called is not an error, although an unused local variable would be.
- Typing a lowercase
"c". The options are capital letters, and strings compare exactly. - Running the code to find out. The quiz is about reading code and predicting its behaviour.
Key takeaways
- A function body runs only when the function is called.
- A definition with no call produces no output and no error.
- Go requires every local variable to be used.