Two "Return" in a function

Asked

Viewed 2,878 times

9

How it works if there are two (+) return in a Python function? It would return the second value, or only the first?

Example:

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    return x
    return y

4 answers

18


I had asked in comment because at first I was too abstract, still remains a little abstract of what you really want, but come on the options.

This is not possible. The return has two functions:

  • establish a value obtained by an expression contained in it, in case used only a variable that will be used as outworking function somewhere else where this function was called;
  • change the code execution flow by immediately finishing the execution of the function being executed and returning control to the place where she was called.

Therefore after it is executed everything that follows in the code will not be executed. In this case the return y will never be executed.

It’s only possible to have more than one return in the code if there is a conditional guard where one or the other will be executed under certain circumstances. There are languages that have even other forms, but not Python.

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    if x > 5:
        return x
    else:
        return y

But if you really want execution to have a way to continue you can use the yield. He’s the same as one return, but it guards the state it was and possibly will continue from where it left off. Maybe that’s what you’re looking for, but it doesn’t look like it. The yield is still a return.

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    yield x
    yield y

But then you can’t call the function anyway, you’d need a generator to control the state of the function.

Unless you want to return two things, and Python was happy to adopt multiple return as a tuple. but I don’t think that’s what you want.

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    return (x, y)

I put in the Github for future reference.

It depends on what your need can adopt each of these strategies to have more than one return, or at least more than one value returned.

9

Your example has no purpose, so there’s no way to give too many details.

If you need to return more than one function value, you can return a sequence such as a list, tuple, set, etc.

def função():
    ...
    return (x, y)

x, y = função()

However, another way is to return a generator function. The generator will also be an eternal object and you can "return" more values through it by simply going through the items it generates:

def função():
    ...
    yield x
    yield y

gen = função()

x = next(gen)
y = next(gen)

7

The return immediately terminates the execution of the function by returning the value in question and the stream to the caller. even a function that does not explicitly define the return will return None.

def grok():
    # faz alguma coisa

>>> valor = grok()
>>> valor is None
True

By immediately terminating the execution of the function other statements below it will never be executed unless there is some conditional test verifying which should be returned.

def recebe_numero(numero):
    if (numero % 2 == 0):
        return "Par"
    else:
        return "Ímpar"

In the above example there are two statements of return but only one will be run depending on the conditional test.

I was reminded by @Andersoncarloswoss that there are situations where Python even after the execution of a return will, shall we say, perform other operations before allowing return to the caller as in the case of the declaration with performing the magic method (Dunder) __exit__ before returning to flow.

6

If I were you I would return a list with the two values, for example:

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    return [x, y]

Or else, Python allows you to Return with a tuple of two values.

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    return x, y
  • That I didn’t know Anderson, thank you very much.

  • 1

    That’s what we call tuple Packing, if you want to search further. See an example: https://repl.it/@acwoss/Adventurousintrepiddisks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.