0

I want to return just 1 value to the function with multiple return values. I tried this:

func myFunc() (int, int){
    return _, 3
}

But it didn't work and raised this error: cannot use _ as value

I already know that It's possible to receive one of the returned values.

Is there any way to return just 1 value?

4
  • 4
    Is there any way to return just 1 value? Plain and simple: No. Commented Oct 10, 2021 at 17:47
  • See proposal: return blank identifier as zero value. Commented Oct 10, 2021 at 17:58
  • Could you elaborate why you would that? Why not remove one return value from signature of you're not using it? Commented Oct 11, 2021 at 15:14
  • I didn't know it's impossible, so I chose wrong algorithm that use both returned values. Commented Oct 13, 2021 at 18:26

1 Answer 1

2

Use the zero value for the other return parameters:

func myFunc() (int, int){
    return 0, 3
}

If you use named result parameters, you may also do:

func myFunc() (x, y int){
    y = 3
    return
}

In this case x will also be the zero value of its type, 0 in case of int.

You could also write a helper function which adds a dummy return value, e.g.:

func myFunc() (int, int) {
    return extend(3)
}

func extend(i int) (int, int) {
    return 0, i
}

But personally I don't think it's worth it. Just return the zero value for "unused" return parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Either one can change function signature, or, if this is an external contract, then the return value is lost likely used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.