Two "return" in a function

8

How does it work if there are two (+) return in a Python function? Would you like to return the second value, or just the first one?

Example:

def soma(a, b):
    x = a + b
    y = 'qualquer coisa'
    return x
    return y
    
asked by anonymous 21.09.2018 / 02:36

4 answers

16

I had asked in comment because at first it was too abstract, it still remains a bit abstract of what it really wants, but let's go the options.

This is not possible. return has two functions:

  • Set a value obtained by an expression contained in it, in case you used only one variable that will be used as a result function somewhere else where this function was called;
  • Change the execution flow of the code by immediately terminating the execution of the function being executed and returning the control to the place where it was called.

So after it runs everything else that follows in the code will not run. In this case the return y will never be executed.

It is only possible to have more than return in the code if there is a conditional guard where one or the other will run under certain circumstances. There are languages that have 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 the execution to have a way to continue you can use yield . It is equal to return , but it saves the state it was in and possibly will continue from where it left off. Maybe that's what you're looking for, but it does not. The yield is nonetheless a return .

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

But there you can not call the function anyway, it would need a generator to control the state of the function.

Unless you want to return two things, and Python was fortunate enough to adopt the multiple return as a tuple. but I do not think this is what you want.

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

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

    
21.09.2018 / 02:49
9

Your example has no purpose, so you can not give too much detail.

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. The generator will also be an iterable object and you can "return" more values through it by simply scrolling through the items it generates:

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

gen = função()

x = next(gen)
y = next(gen)
21.09.2018 / 02:48
7

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

def grok():
    # faz alguma coisa

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

Immediately finishing the execution of the function other declarations below it will never be executed unless there is some conditional test verifying which one 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 executed depending on the conditional test.

I was reminded by @AndersonCarlosWoss that there are situations where Python even after executing a return will, so to do other operations before allowing the caller to return as in the case of the with statement that executes the magic method (dunder) __exit__ before returning to the stream.

    
21.09.2018 / 02:50
5

If I were you 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 lets you do a return with a two-valued tuple.

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