Start learning free

LESSON 41 OF 338 · FUNCTIONS I

Quiz: Forgotten Return in Go

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 getDouble(n int) int {
    n * 2
}

func mainFunction() {
    result := getDouble(10)
    fmt.Println(result)
}

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 Forgotten Return in Go, explained

The question

The quiz shows a function getDouble that takes an int named n and is declared to give back an int. Its body is the single expression n * 2. mainFunction calls getDouble(10), stores the result and prints it. The question is what happens if you run the code.

The options are printing 20, a compile error because the function doesn't return an int, printing 0, and printing 10. You answer by assigning the letter to a variable.

Reasoning it through

Start with the header, func getDouble(n int) int. The second int, after the parameter list, is a promise: this function will give back an integer. In Go, a promise like that is checked by the compiler before anything runs.

Now the body. The line n * 2 computes a number, but nothing is done with it. It isn't returned, stored or printed. Go rejects this on two counts:

  1. A computed value that is not used. Go doesn't accept a bare arithmetic expression as a statement. The compiler reports n * 2 (value of type int) is not used.
  2. A missing return. A function with a result type must end in a return on every path. This body has none, so the compiler reports missing return.

Because compilation fails, the program never starts. That rules out 20, 0 and 10, since there is no output at all. Those answers would be natural in languages where the last expression is the function's value or where a missing return gives a default, but Go has neither rule. The correct option is B, the compile error.

Why Go is strict here

Go checks function results at compile time on purpose. If the compiler allowed a function to end without returning, the caller would receive a value the author never chose, and the bug would surface far from its cause. Rejecting the code immediately points at the function that broke its promise. The same strictness applies to unused local variables and unused imports, which are errors too.

It also helps to know what a correct run looks like. With the fix below, getDouble(10) evaluates to 20, result holds that value, and fmt.Println(result) prints it. None of that happens with the quiz code, because the compiler stops first.

The answer

Following the lesson, the answer goes in a variable declared with var:

var answer string = "B"

The details of this declaration:

  • var answer string declares a variable named answer of type string. The type comes after the name.
  • The value is a string in double quotes. "B" is a string, while B without quotes would be treated as the name of a variable that doesn't exist.
  • Use a capital letter exactly as the option is written.

The fix

Return the value instead of leaving it unused:

func getDouble(n int) int {
    return n * 2
}

With the return, the call getDouble(10) gives 20, and the program prints 20.

Common mistakes

  • Picking A because the intent is clear. A reader can see that the author wanted double the input, but the compiler goes only by the rules. Without return, the code does not build.
  • Expecting a default value. Go does not return 0 automatically when you forget to return. The code is rejected instead.
  • Expecting the last expression to be the result. In Go a function body is a list of statements, and only return sends a value out.
  • Writing the answer without quotes. var answer string = B fails, because B is not defined.
Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free