Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:18:36+00:00 2026-06-14T18:18:36+00:00

Here is the program to find the factorial of a number in Go: func

  • 0

Here is the program to find the factorial of a number in Go:

func factorial(x uint) uint {
    if x == 0 {
        return 1
    }

    return x * (factorial(x - 1))
}

The output for this function when called on input 5 is 120. However, if I add an else statement I get an error.

func factorial(x uint) uint {
    if x == 0 {
        return 1
    } else {
        return x * (factorial(x - 1))
    }
}

Error : function ends without a return statement

I added a return at the end :

func factorial(x uint) uint {
    if x == 0 {
        return 1
    } else {
        return x * (factorial(x - 1))
    }
    fmt.Println("this never executes")
    return 1
}

and I get back the expected output of 120.

Why would the second case cause an error? Why in the third case even though the function never reaches the last return 1, it computes the correct output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:18:38+00:00Added an answer on June 14, 2026 at 6:18 pm

    This is a well known problem of the compiler.

    There is even an issue logged : http://code.google.com/p/go/issues/detail?id=65

    In the words of one of the authors of the Go language:

    The compilers require either a return or a panic to be lexically last
    in a function with a result. This rule is easier than requiring full
    flow control analysis to determine whether a function reaches the end
    without returning (which is very hard in general), and simpler than
    rules to enumerate easy cases such as this one. Also, being purely
    lexical, the error cannot arise spontaneously due to changes in values
    such as constants used in control structures inside the function.

    -rob

    From another comment in golang-nuts, we can infer it’s not going to be “fixed” soon :

    It’s not a bug, it’s a deliberate design decision.

    -rob

    Note that other languages like Java have rules allowing this else.


    March 2013 EDIT – It just got changed in Go1.1 :

    Before Go 1.1, a function that returned a value needed an explicit
    “return” or call to panic at the end of the function; this was a
    simple way to make the programmer be explicit about the meaning of the
    function. But there are many cases where a final “return” is clearly
    unnecessary, such as a function with only an infinite “for” loop.

    In Go 1.1, the rule about final “return” statements is more
    permissive. It introduces the concept of a terminating statement, a
    statement that is guaranteed to be the last one a function executes.
    Examples include “for” loops with no condition and “if-else”
    statements in which each half ends in a “return”. If the final
    statement of a function can be shown syntactically to be a terminating
    statement, no final “return” statement is needed.

    Note that the rule is purely syntactic: it pays no attention to the
    values in the code and therefore requires no complex analysis.

    Updating: The change is backward-compatible, but existing code with
    superfluous “return” statements and calls to panic may be simplified
    manually. Such code can be identified by go vet.

    And the issue I mentioned is now closed with status “Fixed”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there is this check-in-out program here at my workplace, it only takes the data
OK, here is a program that prompts a user for basketball player input. Well
I am writing a program to find adapters, and have made a class called
PLease help me out here. The program is supposed to recursively find out the
This program supposed to find command line arguments entered on Unix which ends with
I wrote a simple program to find the Sum, average, biggest and smallest number
Here is my question, and I have searched this stackoverflow but cannot find the
here is the program which is supposed to find the most common element in
Here is my program to find all the subsets of given set. To solve
I created this test program to find out what happens when a timer callback

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.