Skip to content

Tutorial example with named return values contains incorrect comment and missing return statement #861

@sammedsc45

Description

@sammedsc45

Issue Location

Go Functions Tutorial - Named Return Values Example (https://www.learn-golang.org/en/Functions)

Problem

The tutorial contains an example code showing how to use named return values:

func add (a int, b int) (sum int) {
    sum = a + b                             // so no need for a return statement, go takes care of it
}

This code has TWO critical problems:

  1. Misleading Comment: The comment states "so no need for a return statement, go takes care of it" - This is FALSE
  2. Missing Return Statement: The code is missing the required return keyword

Compilation Error

When executed, this code produces:

missing return at end of function

Root Cause

While named return values allow you to omit the variable name in the return statement (e.g., return instead of return sum), you still MUST have the return keyword.

Correct Code

func add (a int, b int) (sum int) {
    sum = a + b
    return  // The return keyword is REQUIRED
}

Impact

This misleading comment and non-working example:

  • Confuses learners about Go's function requirements
  • Causes runtime compilation errors
  • Violates the tutorial's educational goal of teaching correct Go syntax

Recommended Fix

  1. Remove the misleading comment or correct it to explain that return is still required
  2. Add the missing return statement to make the code compile and run
  3. Update the explanation to clarify that named return values only omit the variable name, not the keyword

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions