ProgramGuru

Named Return Values

Start with Named Return Values

Named return values provide names for the outputs of a function. They are initialized to their zero values when the function is entered.

What you’ll learn

  • Explain the main purpose of named return values.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

In func dimensions() (width, height int), the result variables width and height are available inside the body. A bare return returns their current values. Naming results can make a complex result list easier to understand.

For simple functions, an explicit return width, height often reads more clearly, even when outputs are named. A bare return inside a longer function can make it hard to see which values will be returned.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for named returnsNamed results behave like local variables with zero values.Enter rectangle()Step 1width=0, height=0Step 2Set width and heightStep 3Return bothStep 4Named results behave like local variables with zero values.
Named results behave like local variables with zero values.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func rectangle() (width, height int) {
	width = 8
	height = 3
	return width, height
}
func main() {
	w, h := rectangle()
	fmt.Println("Width:", w)
	fmt.Println("Height:", h)
}

Output

Width: 8
Height: 3

Trace what the program does

On entry, the named result variables begin as zero-value integers. The two assignments set them to 8 and 3, and the explicit return sends these values back to main. The names document which dimension is first.

Avoid common mistakes

  • Named results are not automatically printed or exposed as global variables.
  • Avoid bare returns in long functions; the output may be difficult to discover at the return site.

Try it yourself

Change rectangle to produce width 5 and height 7. Try replacing the explicit return with a bare return and compare the output.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What is the initial value of a named int result?
Choose one answer. You can try again.

Answer and explanation

0

Correct. On entry, the named result variables begin as zero-value integers.

What you should remember

You have practiced Named Return Values in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variadic Functions →