Does every function need a Return at the end of its execution?

Asked

Viewed 67 times

-1

Good morning everyone, I’m starting my studies in python and during the programming of a function that goes in the spreadsheet and transfers the values to a dictionary I came up with a question: All functions need a return?

In my case the dictionary was started in the function init so I’ll want the result after the dictionary is filled with all materials from each sector.

I’ll leave the example of the code in case I haven’t been able to be clear.

class ProdutoPai(object):

def __init__(self, quantidade):
    self.quantidade = quantidade
    self.materiais = {}
    self.chaves = ["Codigo", "Descrição", "Quantidade", "Unidade", "Preço"]

def setor(self):        """
    Essa função acrescenta a self.materiais os materiais usado no DAC para fabricação do modulo base

    :return: None
    """
    wb = load_workbook(self.url)
    ws = wb["setor"]
    linhas = ws.max_row
    prod = self.materiais["produto 1"] = {}

    for linha in range(1, linhas + 1):
       prod[ws["A{}".format(linha)].value] = {self.chaves[0]: ws["A{}".format(linha)].value,
                                             self.chaves[1]: ws["B{}".format(linha)].value,
                                             self.chaves[2]: (ws["D{}".format(linha)].value * self.quantidade),
                                             self.chaves[3]: ws["C{}".format(linha)].value,
                                             self.chaves[4]: (ws["F{}".format(linha)].value *
                                                              (ws["D{}".format(linha)].value * self.quantidade))}
  • It is not necessary, it depends on what you need. Whoever calls the function will need the result of it?

  • In this case not only do I need you to change the value of self.materials, the idea would be to return at the end of the whole change process.

2 answers

1


The answer depends somewhat on what you mean by "needing a return".

If you’re asking if Python always returns some value of a function, the answer is yes. All Python functions return some value, even if this value is null (None).

If you’re asking if Python forces you, programmer, to return some value of a function, the answer is nay. It is not necessary to add an explicit return value; in reality, if you do not add the final line with return algum_valor, Python will implicitly return None.

If you’re asking if makes sense to create a function that does not return any value, the answer is depends on what you want to do. In many cases, we are interested in some "side effect" produced by calling a function, such as modifying a mutable object or saving a file to disk. Python itself displays this behavior in the standard library: the function print returns None, because we are interested in its "side effect" (show text on screen), and not what it returns.

This is the case with your method setor, modifying the dictionary (mutable object) self.materiais directly. As your class maintains a reference to that dictionary, there is no explicit need to return it. When we talk about classes - objects whose state can be accessed and modified during code execution - this is quite common.

That said, it’s worth mentioning that if you have many side effects functions, it can make it difficult for you to understand the logic of your own program. So as much as it is possible, for example, to modify a dictionary internally in a function without returning it, it may be simpler for you, a programmer, to understand and test your own code if the function creates a new dictionary and returns it.

  • Faccioni understood perfectly, really if I keep this way in future modifications and maintenance would be complex for me or for someone who would use the code later, thank you very much for the answer

0

Not necessarily, only if you need the function to return something, for example:

def printMessage():
    print("mensagem")

In that case not the need for return

Browser other questions tagged

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