Start learning free

LESSON 29 OF 338 · FUNCTIONS I

Defining a Function Without Calling It in Go: Quiz

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

func sayMyName() {
    fmt.Println("Heisenberg!")
}

Important Note:

Do not use an actual code editor to get the answer. It would defeat the purpose of the quiz.

Pick your answer

Next lesson

GO · FUNCTIONS I

Quiz Defining Without Calling in Go, explained

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.

  1. Is the code valid? The header has the keyword func, a name and parentheses, and the body is wrapped in braces. The call to fmt.Println is inside the body. Nothing is wrong, so option B is out.
  2. 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 named answer with the type string and 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 the var form.
  • 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 answer before the declaration is a compile error, so the line goes above the Println.
  • The letter is a string, not a bare name. Writing C without 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.
Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free