patternMinor
Named outputs of a function
Viewed 0 times
functionoutputsnamed
Problem
Is there a programming language with named return values?
I imagine something like in the following (syntax to be workshopped):
and then
This would obviate the need to repeat variable names (now done by defining output dataclasses/structs/namedtuples, or by unpacking:
This is a related question, but it only discusses anonymous tuples.
Functions have automatically bound named inputs; why not outputs.
I imagine something like in the following (syntax to be workshopped):
def f(a, b):
export c = a * b
d = 3*c
export e = c + dand then
from f(4, b=5) import *
print(c, e)This would obviate the need to repeat variable names (now done by defining output dataclasses/structs/namedtuples, or by unpacking:
return c,e; c,e=f()).This is a related question, but it only discusses anonymous tuples.
Functions have automatically bound named inputs; why not outputs.
Solution
Golang has this feature (named return values):
package main
import "fmt"
func f(a, b int) (c, e int) {
c = a * b
d = 3*c
e = c + d
return
}
func main() {
fmt.Println(f(4, 5))
}
c and e are named return values of f function.Context
StackExchange Computer Science Q#148135, answer score: 3
Revisions (0)
No revisions yet.