Skip to content

Language should not demand a return statement when using named return values #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gopherbot opened this issue Nov 23, 2009 · 3 comments
Closed
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language

Comments

@gopherbot
Copy link
Contributor

by thwilloch:

func c(n, m int) (r int)  {
    r= 1;
    for i:=0; i!=m; i++ { r*= n-i; r/= i+1}
    return
}
The final return statement should be unnecessary, but is demanded by
the compiler.  With named return values the compiler should be able to make
the return code at the end of the function.
@rsc
Copy link
Contributor

rsc commented Nov 23, 2009

Comment 1:

We thought about this while designing the feature
and decided that since you can still write "return 5"
and not use r, it is important to signal the 
"return the current value of r" case explicitly,
in case you had intended to return 5 and forgot.

Labels changed: added language-change.

Owner changed to [email protected].

Status changed to WontFix.

@gopherbot
Copy link
Contributor Author

Comment 2 by thwilloch:

I think that with named returns you should forbid return with values.
Then you can easily accept functions without return statements.

@rsc
Copy link
Contributor

rsc commented Nov 29, 2009

Comment 3:

Thanks for your input, but we find it very useful to
allow return with values in functions with named
returns.  The names are good documentation,
and the values make it clear what exactly is
being returned.  We've written a lot of Go code
using this style and are quite happy with it.

@bradfitz bradfitz added the LanguageChange Suggested changes to the Go language label Dec 17, 2014
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language
Projects
None yet
Development

No branches or pull requests

3 participants